<?php


namespace App\DAO;

use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\Pagination\Pagination;
use App\Entities\DocumentoGerado\DocumentoGerado;
use App\Mappers\DocumentoGeradoMapper;
use App\Models\DocumentoGeradoModel;
use App\Services\EncryptService;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use CodeIgniter\Model;
use Exception;

class DocumentoGeradoDAO extends DAO
{

    public function getDocumentoGeradoList (?Pagination $pagination, ?QueryFilterCollection $query_filter_collection, ?QueryOrderCollection $query_order_collection = null) : DAOResponse {
        $sql = '
            SELECT 
                documentos_gerados.id as id,
                documentos_gerados.id_cliente as id_cliente,
                documentos_gerados.id_pet as id_pet,
                documentos_gerados.url as url,
                documentos_gerados.descricao as descricao,
                documentos_gerados.criado_em as criado_em
            FROM 
                documentos_gerados
        ';
        return $this->getDAOResponseFromPaginatedSelect([], $pagination, $query_filter_collection, $query_order_collection, $sql, function (array $result_array) {
            return DocumentoGeradoMapper::getDocumentoGeradoListVMCollectionFromArray($result_array);
        });
    }

#--------------------------------------------------------------------------------------------------------------------#

    public function getDocumentoGerado (QueryFilterCollection $query_filter_collection) : DAOResponse {
        $sql = '
            SELECT 
                documentos_gerados.id as id,
                documentos_gerados.id_cliente as id_cliente,
                documentos_gerados.id_pet as id_pet,
                documentos_gerados.url as url,
                documentos_gerados.descricao as descricao,
                documentos_gerados.criado_em as criado_em
            FROM 
                documentos_gerados
        ';
        $binds = [];
        return $this->getDAOResponseFromUniqueRowSelect($binds, $query_filter_collection, $sql, function (array $result_array) {
            $key = "HLuM5EYw5cd0vdJR9yLlM6wt2vte5iniQBqE70nZuhK=";
            $get_file = file_get_contents($result_array[0]["url"]);
            $encrypt = new EncryptService();

            $msg_decrypted = $encrypt->my_decrypt($get_file, $key);
            $base64 = base64_encode($msg_decrypted);

            return DocumentoGeradoMapper::getDocumentoGeradoDetailsVMFromArray($result_array[0], $base64);
        });
    }

# -------------------------------------------------------------------------------------------------------------------------#

    public function getDocumentoGeradoId (int $id_arquivo) {
        $sql = '
            SELECT 
                documentos_gerados.id as id,
                documentos_gerados.id_cliente as id_cliente,
                documentos_gerados.id_pet as id_pet,
                documentos_gerados.url as url,
                documentos_gerados.descricao as descricao,
                documentos_gerados.criado_em as criado_em
            FROM 
                documentos_gerados
            WHERE
                documentos_gerados.id = :id_arquivo:
        ';
        $binds = ['id_arquivo' => $id_arquivo];
        $query = $this->db->query($sql, $binds);
        $result_array = $query->getResultArray();
        return ($result_array[0]['arquivo']);
    }

    public function deleteDocumentoGerado (int $id_galeria) : DAOResponse {
        $arquivo_model = new DocumentoGeradoModel ();
        $dao_response = $this->getDAOResponseForDeletion($arquivo_model, $id_galeria);
        return $dao_response;
    }

}