<?php

namespace App\Controllers;

use App\DAO\Filter\QueryFilter;
use App\DAO\Filter\QueryFilterTranslator;
use App\DAO\Order\QueryOrderTranslator;
use App\DAO\RecepcaoDAO;
use App\Services\AssistenteService;
use App\Services\RecepcaoService;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Validations\RecepcionistaValidation;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;

class RecepcaoController extends BaseController {

	public function registerRecepcionista(): Response {

		if (!$this->validate( RecepcionistaValidation::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'] = '';
		}
		$recepcao_service_response = (new RecepcaoService())->registerRecepcionista( $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' => 'Recepcionista 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( $recepcao_service_response, $response_messages );

	}

	public function getRecepcionistaList(): Response {

		$pagination = $this->getPaginationFromRequest();
		if ($_GET ['sort'] === null) {
			$_GET ['sort'] = "nome";
		}
		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::recepcionista() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::recepcionista() );
		$recepcionista_service_response = (new RecepcaoService())->getRecepcionistaList( $pagination, $query_filter_collection, $query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados dos recepcionistas 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( $recepcionista_service_response, $response_messages );

	}

	public function updateRecepcionistaData(int $id_recepcionista): Response {

		if (!$this->validate( RecepcionistaValidation::update( $id_recepcionista ) )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}
		$recepcao_dao = new RecepcaoDAO();
		$validation = $recepcao_dao->getRecepcionistaFromId( $id_recepcionista );
		if ($validation == null) {
			return $this->response->setJSON( [ 
				'message' => 'O recepcionista não está registrado.',
				'errors' => $this->validation->getErrors()
			] );
		}
		$recepcionista_service_response = (new RecepcaoService())->updateRecepcionistaData( $id_recepcionista, $_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 recepcionista 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( $recepcionista_service_response, $response_messages );

	}

	public function getRecepcionistaByID(int $id_recepcionista): Response {

		$query_filter = QueryFilter::builder()->setNameParam( 'id' )
			->setTable( 'assistente' )
			->setValue( $id_recepcionista )
			->setIsExact( true )
			->build();
		$recepcionista_service_response = (new RecepcaoService())->getRecepcionista( $query_filter );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados da recepcionista 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( $recepcionista_service_response, $response_messages );

	}

}