<?php

namespace App\Controllers;

use App\DAO\AdministrativoDAO;
use App\DAO\Filter\QueryFilter;
use App\DAO\Filter\QueryFilterTranslator;
use App\DAO\Order\QueryOrderTranslator;
use App\Services\AdministrativoService;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Validations\AdministrativoValidation;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Model;

class AdministrativoController extends BaseController {

	public function register(): Response {

		if (!$this->validate( AdministrativoValidation::register() )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}
		$data_post = $_POST;
		if (!$data_post ['complemento']) {
			$data_post ['complemento'] = '';
		}
		if (!$data_post ['referencias']) {
			$data_post ['referencias'] = '';
		}
		$administrativo_service_response = (new AdministrativoService())->registerAdministrativo( $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' => 'O usuário administrativo 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( $administrativo_service_response, $response_messages );

	}

	public function getAdministrativoByID(int $id_administrativo): Response {

		$query_filter = QueryFilter::builder()->setNameParam( 'id' )
			->setTable( 'administrativo' )
			->setValue( $id_administrativo )
			->setIsExact( true )
			->build();
		$administrativo_service_response = (new AdministrativoService())->getAdministrativo( $query_filter );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do usuario administrativo 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( $administrativo_service_response, $response_messages );

	}

	public function getAdministrativoByUserID(int $id_usuario): Response {

		$query_filter = QueryFilter::builder()->setNameParam( 'id' )
			->setTable( 'usuario' )
			->setValue( $id_usuario )
			->setIsExact( true )
			->build();
		$administrativo_service_response = (new AdministrativoService())->getAdministrativo( $query_filter );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do usuario administrativo 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( $administrativo_service_response, $response_messages );

	}

	public function getAdministrativoList(): Response {

		$pagination = $this->getPaginationFromRequest();
		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "nome";
		}
		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::administrativo() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::administrativo() );
		$administrativo_service_response = (new AdministrativoService())->getAdministrativoList( $pagination, $query_filter_collection, $query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do usuario administrativo 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( $administrativo_service_response, $response_messages );

	}

	public function updateAdministrativoData(int $id_administrativo): Response {

		if (!$this->validate( AdministrativoValidation::update( $id_administrativo ) )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}
		$paciente_dao = new AdministrativoDAO();
		$validation = $paciente_dao->getAdministrativoFromId( $id_administrativo );
		if ($validation == null) {
			return $this->response->setJSON( [ 
				'message' => 'O usuario administrativo não está registrado.',
				'errors' => $this->validation->getErrors()
			] );
		}
		$administrativo_service_response = (new AdministrativoService())->updateAdministrativoData( $id_administrativo, $_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' => 'Os dados do usuario administrativo foram atualizados 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( $administrativo_service_response, $response_messages );

	}

}
