<?php

namespace App\Controllers;

use App\DAO\Filter\QueryFilter;
use App\DAO\Filter\QueryFilterTranslator;
use App\DAO\Order\QueryOrderTranslator;
use App\Services\ProcedimentoService;
use App\Services\PagamentoService;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Validations\PagamentoValidation;
use App\Validations\ProcedimentoValidation;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;

class PagamentoController extends BaseController {

	public function registerPagamento(): Response {

		$post = $this->request->getBody();
		$pagamento = json_decode( $post );

		if (!isset( $pagamento->formas_pagamento ) || !is_array( $pagamento->formas_pagamento )) {

			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Forma de pagamento não informada'
			] );
		}

		if (!isset( $pagamento->comanda_itens ) || !is_array( $pagamento->comanda_itens )) {

			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Itens para pagamento não informados'
			] );
		}

		$pagamento_service = new PagamentoService();

		$valorTotal = strval( $pagamento->valor_total );
		$valorTotal = str_replace( ',', '.', $valorTotal );
		$pagamento->valor_total = $valorTotal;
		// $arr = $pagamento->formas_pagamento;
		$arr = $pagamento_service->formatValores( $pagamento->formas_pagamento );

		$pagamento->formas_pagamento = $arr;

		$pagamento_service_response = $pagamento_service->registerPagamento( $pagamento );
		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.',
				'resultado' => $pagamento_service_response->getData()
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'O procedimento foi cadastrado 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( $pagamento_service_response, $response_messages );

	}

	public function getProcedimentoList(): Response {

		$pagination = $this->getPaginationFromRequest();

		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "descricao";
		}

		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::procedimento() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::procedimento() );
		$procedimento_service_response = (new ProcedimentoService())->getProcedimentoList( $pagination, $query_filter_collection, $query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados dos procedimentos 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( $procedimento_service_response, $response_messages );

	}

	public function getProcedimentoConsultaList(): Response {

		$pagination = $this->getPaginationFromRequest();

		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "descricao";
		}

		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::procedimento() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::procedimento() );
		$procedimento_service_response = (new ProcedimentoService())->getProcedimentoConsultaList( $pagination, $query_filter_collection,
			$query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados dos procedimentos 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( $procedimento_service_response, $response_messages );

	}

	public function getProcedimentoInternacaoList(): Response {

		$pagination = $this->getPaginationFromRequest();

		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "descricao";
		}

		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::procedimento() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::procedimento() );
		$procedimento_service_response = (new ProcedimentoService())->getProcedimentoInternacaoList( $pagination, $query_filter_collection,
			$query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados dos procedimentos 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( $procedimento_service_response, $response_messages );

	}

	public function getProcedimentoMedicacaoList(): Response {

		$pagination = $this->getPaginationFromRequest();

		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "descricao";
		}

		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::procedimento() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::procedimento() );
		$procedimento_service_response = (new ProcedimentoService())->getProcedimentoMedicacaoList( $pagination, $query_filter_collection,
			$query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados dos procedimentos 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( $procedimento_service_response, $response_messages );

	}

	public function getProcedimentoCirurgiaList(): Response {

		$pagination = $this->getPaginationFromRequest();

		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "descricao";
		}

		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::procedimento() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::procedimento() );
		$procedimento_service_response = (new ProcedimentoService())->getProcedimentoCirurgiaList( $pagination, $query_filter_collection,
			$query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados dos procedimentos 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( $procedimento_service_response, $response_messages );

	}

	public function searchProcedimentoByID(int $id_procedimento): Response {

		$query_filter = QueryFilter::builder()->setTable( 'procedimento' )
			->setNameParam( 'id' )
			->setValue( $id_procedimento )
			->setIsExact( true )
			->build();
		$procedimento_service_response = (new ProcedimentoService())->getProcedimento( $query_filter );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do procedimento 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( $procedimento_service_response, $response_messages );

	}

	public function deleteProcedimento(int $id_procedimento) {

		$procedimento_service_response = (new ProcedimentoService())->deleteProcedimento( $id_procedimento );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'A exclusão foi efetuda com sucesso.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_PARTIAL_CONTENT,
				'message' => 'Nenhum registro foi encontrado.'
			],
			ServiceResponseInterface::RECORD_IS_LINKED => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Esse de procedimento está vinculado a outros cadastros do sistema. Portanto, não pode ser excluído.'
			],
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro interno do servidor.'
			]
		];
		return $this->getResponse( $procedimento_service_response, $response_messages );

	}

	public function updateProcedimento(int $id_procedimento): Response {

		if (!$this->validate( ProcedimentoValidation::update() )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}
		$procedimento_service_response = (new ProcedimentoService())->updateProcedimentoData( $id_procedimento, $_POST );
		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_PARTIAL_CONTENT,
				'message' => 'O procedimento não foi encontrado.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do procedimento foram atualizados com sucesso.'
			],
			ServiceResponseInterface::VALIDATION_PARAMS_ERROR => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			]
		];
		return $this->getResponse( $procedimento_service_response, $response_messages );

	}

	public function getProcedimentoFilteredByUnidadeAndConvenio() {

		$uri = explode( '/', $_SERVER ['REQUEST_URI'] );
		$unidadeId = ( int ) $uri [3] ?? 0;
		$convenioId = ( int ) $uri [5] ?? 0;
		if (!$unidadeId || !$convenioId) {
			return $this->response->setStatusCode( 404 );
		}
		$sql = <<<SQL
		    select
		        p.id as procedimento_id
		        ,p.descricao as procedimento_descricao
		        ,p.id_tipo_procedimento as tipo_procedimento_id
		        ,tp.descricao as tipo_procedimento_descricao
		        ,cp.id_convenio as convenio_id
		        ,cp.id_unidade as unidade_id
		    from procedimento p 
		    inner join convenio_procedimento cp ON cp.id_procedimento = p.id
		    inner join tipo_procedimento tp on tp.id = p.id_tipo_procedimento 
		    where
		        cp.id_convenio = :convenio_id:
		        and cp.id_unidade = :unidade_id:
		        and cp.desativado_em is null
		        and cp.desativado_por is null
		    order by tp.descricao, p.descricao 
		SQL;
		$data = db_connect()->query( $sql, [ 
			'unidade_id' => $unidadeId,
			'convenio_id' => $convenioId
		] )
			->getResultArray();
		return $this->response->setStatusCode( count( $data ) > 0 ? 200 : 404 )
			->setJSON( [ 
			'message' => 'Dados encontrados com sucesso.',
			'data' => $data
		] );

	}

}
