<?php

namespace App\DAO;

use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\Entities\CEP\CEP;
use App\Mappers\CEPMapper;
use App\Models\CEPModel;
use App\Services\LogService;

class CEPDAO extends DAO {

	public function getDatabaseCEP(string $cep): DAOResponse {

		/*
		 * $dao_response = new DAOResponse();
		 * $sql = " select id,cep,bairro,logradouro,cidade,estado from CEP where cep like '%60532-670%' LIMIT 0, 1;";
		 * $query = $this->db->query( $sql, [ ] );
		 * $result_array = $query->getResultArray();
		 */
		$cep_model = new CEPModel();
		$cep = $cep_model->getCep( $cep );
		$dao_response_status = $cep ? DAOResponseInterface::SUCESS : DAOResponseInterface::RECORD_NOT_FOUND;
		$dao_response = DAOResponse::builder()->setStatus( $dao_response_status )->build();
		if ($dao_response_status) {
			$dao_response->setData( $cep );
		}
		return $dao_response;

	}

	public function RegisterCEP(CEP $cep): DAOResponse {

		$cep_model = new CEPModel();
		$cep_array = $cep->toArray();
		$insert_status = $cep_model->insert( $cep_array );
		$dao_response_status = $insert_status !== false ? DAOResponseInterface::SUCESS : DAOResponseInterface::DATABASE_CONNECTION_ERROR;
		if ($dao_response_status === DAOResponseInterface::SUCESS) {
			log_message( 'info', "O cep {$cep->getCep()} foi cadastrado." );
		} else {
			$log_service = new LogService();
			$log_service->log_error( $dao_response_status, 'Aconteceu um erro no cadastro do cep.', $cep_array );
		}
		return DAOResponse::builder()->setStatus( $dao_response_status )->build();

	}

}