<?php namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\InternacaoDAO;
use App\DAO\Pagination\Pagination;
use App\Mappers\FormularioMapper;
use App\Mappers\InternacaoMapper;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Utils\ResponseException;

class InternacaoService extends Service
{
    public function registerAltaInternacaoMedicaObito ($procedimento, $id_cliente, $id_pet, $id_unidade, array $data_post) : ServiceResponse {

/*        $service_response = new ServiceResponse();
        // Verifica erros de validação na construção do objeto de entrada de dados (FORMULARIO)
        try {
            $formulario_registration_vm = FormularioMapper::getFormularioRegistrationVMFromArray( $data_request );
        } catch ( ResponseException $exception ) {
            $service_response->setErrors( $exception->getData() );
            $service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
            return $service_response;
        }*/


        //$internacao_registration_vm = InternacaoMapper::getInternacaoRegistrationVMFromArray($data_request);
        $internacao_dao = new InternacaoDAO();
        $dao_response = $internacao_dao->persistSaidaInternacaoAltaObitoRegistration($procedimento, $id_cliente, $id_pet, $id_unidade, $data_post);
        $service_response = new ServiceResponse();
        switch ($dao_response->getStatus()) {
            case DAOResponseInterface::SUCESS:
                $data = [];
                $service_response->setStatus(ServiceResponseInterface::SUCESS);
                $service_response->setData($data);
                break;
            case DAOResponseInterface::FOREIGN_KEY_DONT_EXISTS:
            case DAOResponseInterface::DUPLICATED_UNIQUE_KEY:
                $service_response->setStatus(ServiceResponseInterface::VALIDATION_PARAMS_ERROR);
                $service_response->setErrors($dao_response->getErrors());
                break;
            case DAOResponseInterface::DATABASE_CONNECTION_ERROR:
                $service_response->setStatus(ServiceResponseInterface::DATABASE_CONNECTION_ERROR);
                break;
        }
        return $service_response;
    }

    public function getInternacaoList (?Pagination $pagination, ?QueryFilterCollection $query_filter_collection, ?QueryOrderCollection $query_order_collection, $id_unidade) : ServiceResponse{
        $internacao_dao = new InternacaoDAO();
        $dao_response = $internacao_dao->getInternacaoList($pagination, $query_filter_collection, $query_order_collection, $id_unidade);
        switch ($dao_response->getStatus()) {
            case DAOResponseInterface::SUCESS:
                $service_response_status = ServiceResponseInterface::SUCESS;
                break;
            case DAOResponseInterface::RECORD_NOT_FOUND:
                $service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
                break;
            case DAOResponseInterface::DATABASE_CONNECTION_ERROR:
                $service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
                break;
        }
        $service_response = ServiceResponse::builder()
            ->setStatus($service_response_status)
            ->build();
        if ($service_response_status === DAOResponseInterface::SUCESS) {
            $dao_response_data = $dao_response->getData();
            $internacao_details_array = InternacaoMapper::getInternacaosDetailsArrayFromInternacaoDetailsVMCollection($dao_response_data);
            $pagination = $dao_response->getPagination();
            $data = [
                'internacao' => $internacao_details_array,
                'pagination' => $pagination ? $pagination->toResponseArray() : null
            ];
            $service_response->setData($data);
        }
        return $service_response;
    }
}