<?php

namespace App\Controllers;

use App\DAO\Filter\QueryFilter;
use App\DAO\Filter\QueryFilterTranslator;
use App\DAO\Order\QueryOrderTranslator;
use App\Services\ArquivoService;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Validations\ArquivoValidation;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Paths;
use Exception;

class ArquivoController extends BaseController {

	function registerArquivo() {

		$data_post = $this->getDataPost();
		if (!$this->validate( ArquivoValidation::register() )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}
		if (!isset( $_FILES ['arquivo'] ) || $_FILES ['arquivo'] ['name'] == '') {
			return $this->response->setJSON( [ 
				'message' => 'Nenhum arquivo enviado.',
				'errors' => $this->validation->getErrors()
			] );
		}

		try {
			$assistente_service_response = (new ArquivoService())->registerArquivo( $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 arquivo 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( $assistente_service_response, $response_messages );
		} catch ( Exception $e ) {
			return (json_encode( $e->getMessage() ));
		}

	}

	function registerArquivoExame() {

		$data_post = $this->getDataPost();
		if (!$this->validate( ArquivoValidation::register() )) {
			return $this->response->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'errors' => $this->validation->getErrors()
			] );
		}
		if (!isset( $_FILES ['arquivo'] ) || $_FILES ['arquivo'] ['name'] == '') {
			return $this->response->setJSON( [ 
				'message' => 'Nenhum arquivo enviado.',
				'errors' => $this->validation->getErrors()
			] );
		}

		try {
			$assistente_service_response = (new ArquivoService())->registerArquivoExame( $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 arquivo 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( $assistente_service_response, $response_messages );
		} catch ( Exception $e ) {
			return (json_encode( $e->getMessage() ));
		}

	}

	function getArquivoList() {

		$pagination = $this->getPaginationFromRequest();
		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::arquivo() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::arquivo() );
		$procedimento_service_response = (new ArquivoService())->getArquivoList( $pagination, $query_filter_collection, $query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados da galeria 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 );

	}

	function getArquivoListExame() {

		$pagination = $this->getPaginationFromRequest();
		$query_filter_collection = new QueryFilterCollection( $_GET, QueryFilterTranslator::arquivoExame() );
		$query_order_collection = new QueryOrderCollection( $_GET, QueryOrderTranslator::arquivoExame() );
		$procedimento_service_response = (new ArquivoService())->getArquivoListExame( $pagination, $query_filter_collection, $query_order_collection );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados da galeria 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 searchArquivoByID(int $id_arquivo): Response {

		$query_filter = QueryFilter::builder()->setTable( 'arquivo' )
			->setNameParam( 'id' )
			->setValue( $id_arquivo )
			->setIsExact( true )
			->build();
		$erro_service_response = (new ArquivoService())->getArquivo( $query_filter );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do exame 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 );

	}

	public function searchArquivoExameByID(int $id_arquivo): Response {

		$query_filter = QueryFilter::builder()->setTable( 'arquivo_exame' )
			->setNameParam( 'id' )
			->setValue( $id_arquivo )
			->setIsExact( true )
			->build();
		$erro_service_response = (new ArquivoService())->getArquivoExame( $query_filter );
		$response_messages = [ 
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Os dados do exame 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 );

	}

	public function deleteArquivo(int $id_galeria) {

		$erro_service_response = (new ArquivoService())->deleteArquivo( $id_galeria );
		$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 foto 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 );

	}

	public function deleteArquivoExame(int $id_galeria) {

		$erro_service_response = (new ArquivoService())->deleteArquivoExame( $id_galeria );
		$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 foto 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 );

	}

}
