<?php
namespace Modules\Signature\Controller;

use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Trad\Trad;
use Modules\GlobalApplication\Repository\Signature;
use Modules\Commande\Repository\Commande;
use Modules\DemandeAchat\Repository\DemandeAchat;
use Modules\Facture\Repository\BonAPayerRepo;
use Helpers\Session\Enums\MenuTopEnum;
use Helpers\Session\Session;

class DelegationSignatureController extends Controller
{
    /**
     * @var string $menuActive
     */
    public $menuActive;

    public function init() : void
    {
        $this->menuActive = Session::verifySessionTypeMenuTop(MenuTopEnum::P2P) ? 'eproc' : 'CollecteBesoins';
    }


    public function delegationAction() : string
    {
        if (!Session::verifySessionTypeMenuTop(MenuTopEnum::P2P) && !Session::verifySessionTypeMenuTop(MenuTopEnum::S2C)) {
            Session::setSessionTypeMenuTop();
            $this->menuActive = 'eproc';
        }

        $post = $this->request->getParsedBody();
        $module = $this->request->getAttribute('MODULE');
        
        $signature = new Signature($this->pdo);
        $listeDelegation= $signature->getListeDelegateurs($post);

        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeDelegation, 0, $post, $this->renderer);

        $trad = $this->getTranslationService();
        $repoCommande = new Commande($this->pdo);
        $repoDemandeAchats = new DemandeAchat($this->pdo);
        $bapRepo = new BonAPayerRepo($this->pdo);


        if ($module=='Commande') {
            $mapSignatByDeleg = array_column($repoCommande->getCountSignatByDeleg(array_column($listeDelegation, 'SIGNAT')), null, 'SIGNAT');
            foreach ($listeDelegation as &$item) {
                $item['nbSign'] = $mapSignatByDeleg[ $item['SIGNAT'] ]['count'] ?? null;
            }
            
            
            Ariane::generate(0, $trad->t('Délégation des commandes'), $post, count($listeDelegation));
            $titre = $trad->t('Vos délégations de commandes');
        } elseif ($module=='demandeAchat') {
            $mapSignatByDeleg = array_column($repoDemandeAchats->getCountSignatByDeleg(array_column($listeDelegation, 'SIGNAT')), null, 'SIGNAT');
            foreach ($listeDelegation as &$item) {
                $item['nbSign'] = $mapSignatByDeleg[ $item['SIGNAT'] ]['count'] ?? null;
            }

            Ariane::generate(0, $trad->t('Délégation des demandes d\'achat'), $post, count($listeDelegation));
            $titre = $trad->t('Vos délégations de demandes d\'achat');
        } elseif ($module=='facture') {
            $mapSignatByDeleg = array_column($bapRepo->getCountSignatByDeleg(array_column($listeDelegation, 'SIGNAT')), null, 'SIGNAT');
            foreach ($listeDelegation as &$item) {
                $item['nbSign'] = $mapSignatByDeleg[ $item['SIGNAT'] ]['count'] ?? null;
            }

            Ariane::generate(0, $trad->t('Délégation des factures'), $post, count($listeDelegation));
            $titre = $trad->t('Vos délégations de factures');
        } else {
            Ariane::generate(0, $trad->t('Vos délégations'), $post, count($listeDelegation));
            $titre = $trad->t('Vos délégations reçues');

            $mapSignatByDelegCDE = array_column($repoCommande->getCountSignatByDeleg(array_column($listeDelegation, 'SIGNAT')), null, 'SIGNAT');
            $mapSignatByDelegDA = array_column($repoDemandeAchats->getCountSignatByDeleg(array_column($listeDelegation, 'SIGNAT')), null, 'SIGNAT');
            $mapSignatByDelegBAP = array_column($bapRepo->getCountSignatByDeleg(array_column($listeDelegation, 'SIGNAT')), null, 'SIGNAT');
            foreach ($listeDelegation as &$item) {
                $item['nbSignCde'] = $mapSignatByDelegCDE[ $item['SIGNAT'] ]['count'] ?? 0;
                $item['nbSignDa'] = $mapSignatByDelegDA[ $item['SIGNAT'] ]['count'] ?? 0;
                $item['nbSignFac'] = $mapSignatByDelegBAP[ $item['SIGNAT'] ]['count'] ?? 0;
            }
        }
        
        return $this->renderer->render('@signature/Delegation/delegation', [
            'menuActive' => $this->menuActive,
            'menuLeft' => $module=='all'? '':'deleg_'.$module,
            'titre' => $titre,
            'infoPagination' => $infopagination,
            'listeDelegation' => $listeDelegation,
            'module' => $module,
        ]);
    }
}
