<?php

namespace App\DAO;

use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\Infra\K2web\K2web;
use App\Services\JWT\UsuarioLogado;
use App\Utils\DateUtils;

class AgendamentoVoucherDAO extends DAO {

	public function register($agendamento, $voucher): DAOResponse {

		$usuario = UsuarioLogado::getUsuarioId();
		$daoResponse = new DAOResponse();
		$this->db->transStart();

		$sql_status = <<<SQL
		    update agendamento_especialidade set id_status = 2
		    where id_requisicao_procedimento = (select id_requisicao from agendamento where agendamento.id = :id_agendamento:) 
		SQL;
		$binds_status = [ 
			'id_agendamento' => $agendamento
		];
		// $this->db->query( $sql_status, $binds_status );
		$conn_status = db_connect();
		$result_status = $conn_status->query( $sql_status, $binds_status )->numRows;
		$errors_status = $conn_status->error();
		if ($errors_status ['code'] != 0) {
			$daoResponse->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$daoResponse->setErrors( $errors_status );
			$daoResponse->setData( [ ] );
			$this->db->transRollback();
			return $daoResponse;
		}

		$sql_voucher = <<<SQL
		    update voucher set id_status = 5 
		    where id = :id_voucher: 
		SQL;
		$binds_voucher = [ 
			'id_voucher' => $voucher
		];
		// $this->db->query( $sql_voucher, $binds_voucher );

		$conn_voucher = db_connect();
		$result_voucher = $conn_voucher->query( $sql_voucher, $binds_voucher )->numRows;
		$errors_voucher = $conn_voucher->error();

		if ($errors_voucher ['code'] != 0) {
			$daoResponse->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$daoResponse->setErrors( $errors_voucher );
			$daoResponse->setData( [ ] );
			$this->db->transRollback();
			return $daoResponse;
		}

		$sql = <<<SQL
			insert into agendamento_voucher
				(id_agendamento, id_voucher, criado_por, data_criacao, hora_criacao)
			values
				(:id_agendamento:, :id_voucher:, :criado_por:, :data_c:,:hora_c: )	
		SQL;

		$binds = [ 
			'id_agendamento' => $agendamento,
			'id_voucher' => $voucher,
			'criado_por' => $usuario,
			'data_c' => DateUtils::today(),
			'hora_c' => DateUtils::now_time()
		];
		$affectrows = 0;
		$conn = db_connect();
		$result = $conn->query( $sql, $binds )->numRows;
		$errors = $conn->error();
		$affectrows = $conn->affectedRows();
		// $numrows = $this->db->query( $sql, $binds )->numRows;
		// $errors = $this->db->error();

		$mensagem = new K2web();
		$mensagem->enviarMensagem( 2, $agendamento );

		if ($errors ['code'] != 0) {
			$daoResponse->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$daoResponse->setErrors( $errors );
			$daoResponse->setData( [ ] );
			$this->db->transRollback();
		} else {
			// $affectrows = $this->db->affectedRows();

			if ($affectrows == 0) {
				$daoResponse->setStatus( DAOResponseInterface::INSUFFICIENT_DATA );
				$daoResponse->setData( [ ] );
				$this->db->transRollback();
			} else {
				// $newid = $this->db->insertID();
				$newid = $conn->insertID();

				$this->db->transComplete();
				$daoResponse->setStatus( DAOResponseInterface::SUCESS );
				$daoResponse->setData( [ 
					'id_compra' => $newid
				] );
			}
		}

		return $daoResponse;

	}

	public function getVoucherIdByAgendamentoId(int $id_agendamento): DAOResponse {

		$daoResponse = new DAOResponse();

		$sql = <<<SQL
			SELECT 
				id_voucher				
			FROM
				agendamento_voucher
			WHERE
				id_agendamento = :agendamento_id:
				
		SQL;
		$binds = [ 
			'agendamento_id' => $id_agendamento
		];

		$conn = db_connect();
		$result = $conn->query( $sql, $binds )
			->getResultArray();
		$errors = $conn->error();

		if ($errors ['code'] != 0) {
			$daoResponse->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$daoResponse->setErrors( $errors );
			$daoResponse->setData( [ ] );
		} else {

			if (count( $result ) == 0) {
				$daoResponse->setStatus( DAOResponseInterface::RECORD_NOT_FOUND );
				$daoResponse->setData( [ ] );
			} else {
				$daoResponse->setStatus( DAOResponseInterface::SUCESS );
				$daoResponse->setData( $result [0] );
			}
		}

		return $daoResponse;

	}

}





