<?php

namespace Modules\Evaluation\Controller;

use Entity\Model\CampaignModel;
use Entity\Model\CampAsso;
use Entity\Model\CampEnt;
use Entity\Model\CampEve;
use Entity\Model\CampGrpNot;
use Entity\Model\CampLstFour;
use Entity\Model\CampLstNotCamp;
use Entity\Model\CampLstNotFour;
use Entity\Model\CampLstNotGrp;
use Entity\Model\CampNotFin;
use Entity\Model\CampQstDep;
use Entity\Model\CampQstQ;
use Entity\Model\CampQstR;
use Entity\Model\CampQstTh;
use Entity\Model\CampSoc;
use Entity\Model\Periodes;
use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Ariane\ArianeSub;
use Helpers\ExportTo\Outils;
use Helpers\Fichier\Fichier;
use Helpers\Filter\FiltreAvance\Generator;
use Helpers\Filter\FiltreAvance\GeneratorCs;
use Helpers\Outil\ArrayHelper;
use Helpers\Outil\Date;
use Helpers\Outil\Outils as OutilOutils;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Pagination\PaginatorGestion;
use Helpers\Trad\Trad;
use Modules\Evaluation\MenuContextuel\CampaignMenuContextuel;
use Modules\Evaluation\Repository\CampaignEventRepo;
use Modules\Evaluation\Service\ExportService;
use Skeleton\Controller\Controller;
use Modules\Evaluation\MenuContextuel\GestionMenuContextuel;
use Modules\Evaluation\Repository\Association;
use Modules\Evaluation\Repository\Campaign;
use Modules\Evaluation\Repository\Category;
use Modules\Evaluation\Repository\Evaluation;
use Modules\Evaluation\Service\CampaignService;
use Modules\Evaluation\Validator\CampaignValidator;
use Modules\Fournisseur\Repository\GestFourniss;
use PDO;
use PhpOffice\PhpSpreadsheet\IOFactory;
use TwigExtension\OutilExtension;
use Helpers\Session\Enums\MenuTopEnum;
use Helpers\Session\Session;

class CampaignEventController extends Controller
{

    public function monitoringAllAction(): string
    {
        return $this->monitoringPage();
    }

    public function monitoringAction(): string
    {
        return $this->monitoringPage($this->request->getAttribute('id'));
    }

    private function monitoringPage(?string $id = null): string
    {
        $post = $this->request->getParsedBody();

        if (empty(ArrayHelper::filterByKey($post, function ($key) {
            return str_starts_with($key, 'tri_');
        }))) {
            $post['tri_ID_CAMP'] = 'desc';
            $post['tri_NUM_ORDR'] = 'desc';
        }

        $paginator = new PaginatorGestion();
        $paginator->generate('', 'DTE_EVE', 'ASC');

        return $this->renderer->render('@Evaluation/Campaign/monitoring', [
            'menuActive' => Session::verifySessionTypeMenuTop(MenuTopEnum::S2C) ? 'Consultations' : 'Evaluations',
            'menuLeft' => !empty($id) ? 'ratings' : 'monitoring',
            'eventList' => (new CampaignEventRepo($this->pdo))->eventList($id, $post),
            'campaign' => $id ? (new Campaign($this->pdo))->campaignDetail(intval($id)) : [],
            'post' => $post,
            'infoPagination' => PaginatorGestion::getInfoPagination()
        ]);
    }


    public function exportAllAction() :string
    {
        return $this->export(
            $this->request->getParsedBody()
        );
    }

    public function exportAction() :string
    {
        return $this->export(
            ['ID_CAMP' => $this->request->getAttribute('id')]
            + $this->request->getParsedBody()
        );
    }

    /**
     * @param array<mixed> $post
     */
    private function export(array $post) :string
    {
        
        $trad = $this->getTranslationService();
        $this->getEmailCsvService()->sendCsv(
            'export_camp_event_'.date('Ymd'),
            (new CampaignEventRepo($this->pdo))->export($post),
            function (array $row) {
                return [
                    OutilExtension::entityLabel($row, 'ID_CAMP', 'Camp_LIBELLE'),
                    $row['COD_EVE'],
                    $row['LIBELLE'],
                    Date::dateConverte($row['DTE_EVE']),
                    Date::heureTrad($row['HEU_EVE']),
                    OutilExtension::entityLabel($row, 'ID_USER', 'Auteur_NOM'),
                    $row['COMMENT'] . PHP_EOL
                    . $row['COMMENTAIRE'],
                ];
            },
            [
                $trad->t('Campagne'),
                $trad->t('Code'),
                $trad->t('Évènement'),
                $trad->t('Date'),
                $trad->t('Heure'),
                $trad->t('Auteur'),
                $trad->t('Commentaire'),
            ],
        );
        return 'ok';
    }
}
