<?php

namespace Helpers\Session;

use Modules\Commande\Repository\Commande;
use Modules\Consultation\Repository\GestionConsultation;
use Modules\DemandeAchat\Repository\DemandeAchat;
use Modules\Facture\Repository\BonAPayerRepo;
use Modules\GlobalApplication\Repository\Signature;
use Modules\GlobalApplication\Repository\Utilisateur;
use Modules\Panier\Service\PanierEnteteService;

class InitSession
{
    private \PDO $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * @return string|int
     */
    public static function initSignDemandeAchat(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['totalNoDaToSign']) || $force) {
            $_SESSION['notif']['totalNoDaToSign'] = (new DemandeAchat($pdo))->countAwaitingSign($_SESSION['Session_Login']);
        }

        return $_SESSION['notif']['totalNoDaToSign'];
    }

    /**
     * @return string|int
     */
    public static function initSignCommande(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['totalNoCdeToSign']) || $force) {
            $_SESSION['notif']['totalNoCdeToSign'] = (new Commande($pdo))->countSignat($_SESSION['Session_Login']);
        }

        return $_SESSION['notif']['totalNoCdeToSign'];
    }

    /**
     * @return string|int
     */
    public static function initSigFacture(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['totalNoBapToSign']) || $force) {
            $_SESSION['notif']['totalNoBapToSign'] = (new BonAPayerRepo($pdo))->countFactureSignableCurrent();
        }

        return $_SESSION['notif']['totalNoBapToSign'];
    }

    /**
     * @return string|int
     */
    public static function initCdeToPrint(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['getNbCdeToPrint']) || $force) {
            $_SESSION['notif']['getNbCdeToPrint'] = (new Commande($pdo))->countCdeSend($_SESSION['Session_Login']);
        }

        return $_SESSION['notif']['getNbCdeToPrint'];
    }

    /**
     * @return string|int
     */
    public static function countDeleg(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['getNbItemIndelegationToSign']) || $force) {
            $signature = new Signature($pdo);
            $result = $signature->getListeDelegateurs([]);
            
            $listDeleg = array_column($result, 'SIGNAT');


            $repoBap = new BonAPayerRepo($pdo);
            $delegFac = $repoBap->countFactureSignable(
                null,
                $listDeleg
            );

            $commande = new Commande($pdo);
            $listeDelegCde = $commande->getCountSignatByDeleg($listDeleg);
            $delegCde = 0;
            foreach ($listeDelegCde as $d) {
                $delegCde += (int)$d['count'];
            }

            $repoDemandeAchats = new DemandeAchat($pdo);
            $listeDelegDA = $repoDemandeAchats->getCountSignatByDeleg($listDeleg);
            $delegDa = 0;
            foreach ($listeDelegDA as $d) {
                $delegDa += (int)$d['count'];
            }
            
            $_SESSION['notif']['getNbItemIndelegationToSign'] = $delegFac+$delegCde+$delegDa;
        }
        
        return $_SESSION['notif']['getNbItemIndelegationToSign'];
    }

    /**
     * @return string|int
     */
    public static function initCdeToNote(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['getNbCdeToRate']) || $force) {
            $utilisateur = new Utilisateur($pdo);
            $_SESSION['notif']['getNbCdeToRate'] = $utilisateur->getNbCdeToRate($_SESSION['Session_Login']);
        }

        return $_SESSION['notif']['getNbCdeToRate'];
    }

    /**
     * @return string|int
     */
    public static function panierBrouillon(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['getPanierBrouillon']) || $force) {
            $nombrePanier = (new Utilisateur($pdo))->countPanierBrouillerTransferer();
            $_SESSION['notif']['getPanierBrouillon'] = $nombrePanier;
        }

        return $_SESSION['notif']['getPanierBrouillon'];
    }

    /**
     * @return string|int
     */
    public static function getNbAOAEnvoyer(\PDO $pdo, bool $force = false)
    {
        if (!isset($_SESSION['notif']['getNbAOAEnvoyer']) || $force) {
            $gestionAO_REPO = new GestionConsultation($pdo);
            $listeConsult = $gestionAO_REPO->listeConsultation([
                'filter_Statut' => 'ENVO',
                'ACHETEUR' => $_SESSION["Session_Login"]
            ]);
            $_SESSION['notif']['getNbAOAEnvoyer'] = count($listeConsult);
        }

        return $_SESSION['notif']['getNbAOAEnvoyer'];
    }

    /**
     * @return string|int
     */
    public static function getCountDelegRef(\PDO $pdo, string $table, bool $force = false)
    {

        /** getListeDelegationFOURNISS */
        $delegationGlobal = new \Modules\GlobalApplication\Repository\Delegation($pdo);
        $nbDelegREF = $delegationGlobal->countDelegationRecue($table);
        
        //getListeDelegationFOURNISS
        $_SESSION['notif']['getListeDelegation'.$table] = $nbDelegREF;

        return $_SESSION['notif']['getListeDelegation'.$table];
    }

    public function initSessionHeader() : void
    {
        /*
            $_SESSION['adminNbInscript'] fait en V1
            $_SESSION['getNbModifToAdmin'] fait en V1
            $_SESSION['getNbBlocagesUtilisateur'] fait en V1
        */

        /** totalNoDaToSign */
        self::initSignDemandeAchat($this->pdo, true);

        /** totalNoCdeToSign */
        self::initSignCommande($this->pdo, true);

        self::initSigFacture($this->pdo, true);

        self::countDeleg($this->pdo, true);

        self::initCdeToPrint($this->pdo, true);

        self::panierBrouillon($this->pdo, true);
        
        /** getNbAOAEnvoyer */
        self::getNbAOAEnvoyer($this->pdo, true);

        self::getCountDelegRef($this->pdo, 'FOURNISS', true);
        
        (new PanierEnteteService($this->pdo))->getTotalPanier();
    }
}
