<?php

namespace Modules\Evaluation\Repository;

use Entity\Model\CampSoc;
use GuzzleHttp\Psr7\Response;
use Helpers\Filter\FilterTable;
use Helpers\Outil\Sql;
use Helpers\Pagination\PaginatorGestion;
use Helpers\QueryBuilder\AbsRepository;
use PDO;
use PDOException;

class CampaignEventRepo extends AbsRepository
{

    /**
     * @param array<mixed> $param
     */
    private function getMainRequest(array &$param) :string
    {
        return "SELECT 
                CAMP_EVE.*,
                " . $this->selectTrad('CAMP_TYP_EVE.LIBELLE') . ",
                Auteur.NOM as Auteur_NOM, 
                CAMP_ENT.LIBELLE as Camp_LIBELLE
            FROM CAMP_EVE with(nolock)
            LEFT JOIN CAMP_ENT with(nolock) ON CAMP_ENT.ID_CAMP = CAMP_EVE.ID_CAMP
            LEFT JOIN CAMP_TYP_EVE with(nolock) 
                ON CAMP_TYP_EVE.COD_EVE = CAMP_EVE.COD_EVE
            " . $this->joinTrad('CAMP_TYP_EVE.LIBELLE', $param) . "
            LEFT JOIN HASTUTIL Auteur with(nolock) 
                ON Auteur.CODE = CAMP_EVE.ID_USER
            WHERE 1=1 ";
    }

    /**
     * @param array<mixed> $param
     * @param array<mixed> $post
     */
    private function applyMainRequestFilter(array &$param, array $post) :string
    {
        $query = '';
        if (!empty($post['ID_CAMP'])) {
            $query .= " AND CAMP_EVE.ID_CAMP = :ID_CAMP";
            $param[':ID_CAMP'] = $post['ID_CAMP'];
        }

        if (isset($post['filter_HEU_EVE']) && $post['filter_HEU_EVE'] != '') {
            $post['filter_HEU_EVE'] = str_replace(':', '', $post['filter_HEU_EVE']);
        }

        if (isset($post['filter_LIBELLE']) && $post['filter_LIBELLE'] != '') {
            $query .= " AND UPPER(CAMP_TYP_EVE.LIBELLE) like UPPER(:LIBELLE) ";
            $param[':LIBELLE'] = '%' . $post['filter_LIBELLE'] . '%';
            unset($post['filter_LIBELLE']);
        }

        if (!empty($post['tri_CAMPAGNE'])) {
            $post['tri_CAMP_ENT.LIBELLE'] = $post['tri_CAMPAGNE'];
            unset($post['tri_CAMPAGNE']);
        }
        if (!empty($post['filter_CAMPAGNE'])) {
            $post['filter_CAMP_ENT.LIBELLE'] = $post['filter_CAMPAGNE'];
            unset($post['filter_CAMPAGNE']);
        }

        return $this->applyFilter($query, $param, $post);
    }

    /**
     * eventList
     *
     * @param  string $campaignId
     * @param  array<mixed> $post
     * @return array<mixed>
     */
    public function eventList(?string $campaignId = null, array $post = []): array
    {
        $params = [];
        $query =
            $this->getMainRequest($params)
            . $this->applyMainRequestFilter(
                $params,
                ($campaignId ? [
                    'ID_CAMP' => $campaignId,
                ] : []) + $post
            );

        $paginatorGestion = new PaginatorGestion();
        $paginatorGestion->injecterSqlCount($query, $post, $params, $this->pdo);

        return $this->fetchAssocAll($query, $params);
    }

    /**
     * @param  array<mixed> $post
     */
    public function export(array $post = []):\PDOStatement
    {
        $params = [];
        return $this->createStatement(
            $this->getMainRequest($params)
            . $this->applyMainRequestFilter($params, $post),
            $params
        );
    }
}
