<?php


namespace App\DAO;



use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\Pagination\Pagination;
use App\Entities\GaleriaClassificacao\GaleriaClassificacao;
use App\Mappers\GaleriaClassificacaoMapper;
use App\Models\GaleriaClassificacaoModel;
use App\Services\LogService;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\ViewModels\GaleriaClassificacao\GaleriaClassificacaoRegistrationViewModel\GaleriaClassificacaoRegistrationViewModel;
use App\ViewModels\GaleriaClassificacao\GaleriaClassificacaoUpdateViewModel\GaleriaClassificacaoUpdateViewModel;
use Exception;

class GaleriaClassificacaoDAO extends DAO
{
    public function persistGaleriaClassificacaoRegistrationVM (GaleriaClassificacaoRegistrationViewModel $classificacao_registration_vm) : DAOResponse {
        $dao_response = new DAOResponse();
        try {
            $classificacao = $this->persistGaleriaClassificacao($classificacao_registration_vm);
            $error = $this->db->error();
            if ($error && $error['code']) {
                $status_persistence = $error['code'];
                $dao_response_errors = [];
//                if (strpos($error['message'],'PROCEDIMENTO.descricao') !== false) {
//                    $dao_response_errors['descricao'] = 'Esse procedimento já existe';
//                }
                $dao_response->setErrors($dao_response_errors);
                log_message('error', json_encode($error));
            } else {
                $status_persistence = !!$classificacao->getId()
                    ? DAOResponseInterface::SUCESS
                    : DAOResponseInterface::DATABASE_CONNECTION_ERROR;
            }
        } catch (Exception $exception) {
            log_message('error', json_encode($this->db->error()));
            $status_persistence = DAOResponseInterface::DATABASE_CONNECTION_ERROR;
            $classificacao = null;
        }
        $this->db->close();
        $dao_response->setStatus($status_persistence);
        if ($status_persistence === DAOResponseInterface::SUCESS) {
            $dao_response->setData($classificacao);
        }
        return $dao_response;
    }

    private function persistGaleriaClassificacao (GaleriaClassificacaoRegistrationViewModel $classificacao_registration_vm) : GaleriaClassificacao {
        $classificacao_model = new GaleriaClassificacaoModel ();
        $classificacao = GaleriaClassificacaoMapper::getGaleriaClassificacaoFromGaleriaClassificacaoRegistrationVM($classificacao_registration_vm);
        $classificacao_model->registerGaleriaClassificacao($classificacao);
        return $classificacao;
    }

    public function getGaleriaClassificacaoList (?Pagination $pagination, ?QueryFilterCollection $query_filter_collection, ?QueryOrderCollection $query_order_collection = null) : DAOResponse {
        $sql = '
            SELECT
                galeria_classificacao.id AS id,
                galeria_classificacao.antes AS antes,
                galeria_classificacao.depois AS depois,
                galeria_classificacao.descricao AS descricao_classificacao,
                galeria_classificacao.id AS imagem_antes,
                (SELECT galeria.descricao FROM galeria WHERE galeria.id = antes) AS descricao_antes,
                galeria_classificacao.id AS imagem_depois,
                (SELECT galeria.descricao FROM galeria WHERE galeria.id = depois) AS descricao_depois
            FROM
                galeria_classificacao, galeria
            WHERE
            	galeria_classificacao.depois = galeria.id
        ';
        return $this->getDAOResponseFromPaginatedSelect([], $pagination, $query_filter_collection, $query_order_collection, $sql, function (array $result_array) {
            return GaleriaClassificacaoMapper::getGaleriaClassificacaoDetailsVMCollectionFromArray($result_array);
        });
    }

    public function deleteGaleriaClassificacao (int $id_procedimento) : DAOResponse {
        $classificacao_model = new GaleriaClassificacaoModel ();
        $dao_response = $this->getDAOResponseForDeletion($classificacao_model, $id_procedimento);
        return $dao_response;
    }

    public function getGaleriaClassificacao (QueryFilterCollection $query_filter_collection) : DAOResponse {
        $sql = '
            SELECT
                exame.id AS id,
                exame.nome AS nome
            FROM
                exame
        ';
        $binds = [];
        return $this->getDAOResponseFromUniqueRowSelect($binds, $query_filter_collection, $sql, function (array $result_array) {
            return GaleriaClassificacaoMapper::getGaleriaClassificacaoDetailsVMFromArray($result_array[0]);
        });
    }

    public function persistGaleriaClassificacaoUpdateVM (int $id_procedimento, GaleriaClassificacaoUpdateViewModel $classificacao_update_vm) : DAOResponse {
        $status_persistence = $this->persistGaleriaClassificacaoFromGaleriaClassificacaoUpdateVM($id_procedimento, $classificacao_update_vm);
        $error = $this->db->error();
        $this->db->close();
        $dao_response = new DAOResponse;
        if ($error && $error['code']) {
            $status_response = $error['code'];
            $dao_response_errors = [];
            $dao_response->setErrors($dao_response_errors);
            (new LogService)->log_error($status_response, "Ocorreu um erro durante a atualização do procedimento $id_procedimento", $classificacao_update_vm->toArray());
        } else {
            $status_response = $status_persistence ? DAOResponseInterface::SUCESS :  DAOResponseInterface::RECORD_NOT_FOUND;
        }
        $dao_response->setStatus($status_response);
        return $dao_response;
    }

    private function persistGaleriaClassificacaoFromGaleriaClassificacaoUpdateVM (int $id_procedimento, GaleriaClassificacaoUpdateViewModel $classificacao_update_vm) : bool {
        $classificacao_model = new GaleriaClassificacaoModel();
        $classificacao = GaleriaClassificacaoMapper::getGaleriaClassificacaoFromGaleriaClassificacaoUpdateVM($classificacao_update_vm);
        $sucess_update = $classificacao_model->updateGaleriaClassificacaoByID($id_procedimento, $classificacao);
        return $sucess_update;
    }

}