<?php

namespace App\Controllers;

use App\DAO\ClassificacaoRiscoDAO;
use App\DAO\Filter\QueryFilter;
use App\DAO\Filter\QueryFilterTranslator;
use App\DAO\Order\QueryOrderTranslator;
use App\Services\ClassificacaoRiscoService;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Validations\ClassificacaoRiscoValidation;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use const False\MyClass\true;

class ClassificacaoRiscoController extends BaseController {

	public function registerClassificacaoRisco(): Response {

		if (!$this->validate( ClassificacaoRiscoValidation::register() )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}
		$data_post = $_POST;
		$classificacao_risco_service_response = (new ClassificacaoRiscoService())->registerClassificacaoRisco( $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::FOREIGN_KEY_DONT_EXISTS => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro ao informar os dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'O classificacao de risco 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( $classificacao_risco_service_response, $response_messages );

	}

	public function getClassificacaoRiscoList(): Response {

		$pagination = $this->getPaginationFromRequest();

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

		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::classificacao_risco() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::classificacao_risco() );
		$classificacao_risco_service_response = (new ClassificacaoRiscoService())->getClassificacaoRiscoList( $pagination, $query_filter_collection,
			$query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados das classificações de riscos 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( $classificacao_risco_service_response, $response_messages );

	}

	public function searchClassificacaoRiscoByID(int $id_classificacao_risco): Response {

		$query_filter = QueryFilter::builder()->setTable( 'classificacao_risco' )
			->setNameParam( 'id' )
			->setValue( $id_classificacao_risco )
			->setIsExact( true )
			->build();

		$classificacao_risco_service_response = (new ClassificacaoRiscoService())->getClassificacaoRisco( $query_filter );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do classificacao de risco 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( $classificacao_risco_service_response, $response_messages );

	}

	public function updateClassificacaoRisco(int $id_classificacao_risco): Response {

		if (!$this->validate( ClassificacaoRiscoValidation::update() )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}

		$classificacao_risco_dao = new ClassificacaoRiscoDAO();
		$validation = $classificacao_risco_dao->getClassificacaoRiscoFromId( $id_classificacao_risco );

		if ($validation == null) {
			return $this->response->setJSON( [ 
				'message' => 'O classificacao de risco não está registrado.',
				'errors' => $this->validation->getErrors()
			] );
		}

		$classificacao_risco_service_response = (new ClassificacaoRiscoService())->updateClassificacaoRiscoData( $id_classificacao_risco, $_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 da classificacao de risco 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( $classificacao_risco_service_response, $response_messages );

	}

	public function deleteClassificacaoRisco(int $id_erro) {

		$erro_service_response = (new ClassificacaoRiscoService())->deleteClassificacaoRisco( $id_erro );
		$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' => 'Essa classificacao de risco está vinculada 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( $erro_service_response, $response_messages );

	}

}
