<?php

namespace App\Controllers;

use App\DAO\Filter\QueryFilterTranslator;
use App\DAO\Order\QueryOrderTranslator;
use App\DAO\Pagination\Pagination;
use App\Services\InternacaoService;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Validations\ConsultaValidation;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;

class InternacaoController extends BaseController {

	public function registerAltaInternacaoMedicaObito($procedimento, $id_cliente, $id_pet, $id_unidade): Response {

		$data_post = $this->getDataPost();

		$validation_errors = ConsultaValidation::validateRequisicaoInternacaoAltaObito( $data_post );

		if (count( $validation_errors )) {
			return $this->response->setStatusCode( ResponseInterface::HTTP_BAD_REQUEST )
				->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $validation_errors
			] );
		}

		$erro_service_response = (new InternacaoService())->registerAltaInternacaoMedicaObito( $procedimento, $id_cliente, $id_pet, $id_unidade, $data_post );
		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'A anamnese foi cadastrada com sucesso.'
			],
			ServiceResponseInterface::VALIDATION_PARAMS_ERROR => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::SERVICE_INTERNAL_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro interno do servidor.'
			]
		];
		return $this->getResponse( $erro_service_response, $response_messages );

	}

	public function getInternacaoList($id_unidade): Response {

		$pagination = $this->getPaginationFromRequest();

//  		if (is_null( $pagination )) {
//  			$pagination = Pagination::builder()->setCurrentPage( 1 )
//  				->setPerPage( 15 )
//  				->build();
//  		}

		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "nome_cliente";
		}
		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::internacao() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::internacao() );
		$erro_service_response = (new InternacaoService())->getInternacaoList( $pagination, $query_filter_collection, $query_order_collection, $id_unidade );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados dos erros foram encontrados com sucesso.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_PARTIAL_CONTENT,
				'message' => 'Nenhum registro foi encontrado.'
			],
			ServiceResponseInterface::SERVICE_INTERNAL_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro interno do servidor.'
			],
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro interno do servidor.'
			]
		];
		return $this->getResponse( $erro_service_response, $response_messages );

	}

}
