<?php

namespace Modules\GlobalApplication\Repository;

use Entity\Model\Paravrac;
use Helpers\Outil\Sql;
use Helpers\QueryBuilder\AbsRepository;

class Delegation extends AbsRepository
{
    /**
     * @param array<mixed> $post
     * @return array<array<string>>
     */
    public function listeDelegationRecue(
        string $module,
        array $post = [],
        bool $sansTri = false
    ) :array {
        $arrayReq = [];
        $req = $this->listeDelegationRecueBaseRequest($module, $arrayReq);

        if (!$sansTri) {
            $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
            $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
            $req .= $filtreAvance->generateSql(1);
            $filtreAvance->pushArray($arrayReq);

            if (!empty($whereArray)) {
                $req .= $whereArray[0].$whereArray['tri'];
                
                if (!empty($whereArray[1])) {
                    $arrayReq = array_merge($arrayReq, $whereArray[1]);
                }
            }
        }
        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * Summary of listeDelegationRecueBaseRequest
     * @param string $module
     * @param array<mixed> $param
     * @return string
     */
    protected function listeDelegationRecueBaseRequest(string $module, array &$param) :string
    {
        $today = date('Ymd');

        $listReq[] = "SELECT distinct 
            SIG_DELEG.SIGNAT, DELEG,
            DTE_DEB, DTE_FIN,
            hasig.NOM, 'ABS' as TYPEDEL
        FROM SIG_DELEG with(nolock)
        LEFT JOIN HASTUTIL hasig with(nolock) 
            on SIG_DELEG.SIGNAT = hasig.CODE 
            and (hasig.SIG_DA = 0x54 or hasig.SIG_CDE = 0x54 
                or hasig.SIG_BAP = 0x54 or hasig.SIG_REF = 0x54)
        WHERE DELEG = :user_0
        AND DTE_DEB <= :today_0
        AND (DTE_FIN >= :today_1 or DTE_FIN = '00000000')";

        $param += [
            ':module' => $module,
            ':user_0' => $_SESSION['Session_Login'],
            ':today_0' => $today,
            ':today_1' => $today,
        ];

        $paravrac = new Paravrac($this->pdo);
        if ($paravrac->getFlagForSig('DELEG_AUTO_SUP_HIER')) {
            $listReq[] = "SELECT 
                HASTUTIL.CODE as SIGNAT,  SUPHIER as DELEG, 
                '00000000', '00000000', 
                NOM, 'HIE' as TYPEDEL
            FROM HASTUTIL with(nolock)
            WHERE SUPHIER = :user_1 
                AND (SIG_DA = 0x54 or SIG_CDE = 0x54 or SIG_BAP = 0x54 or SIG_REF = 0x54)";
            $param += [
                ':user_1' => $_SESSION['Session_Login'],
            ];
        }

        if ($paravrac->getFlagForSig('DELEG_AUTO_RESP_UO')) {
            $listReq[] = "SELECT 
                HASTUTIL.CODE as SIGNAT, UO.RESP_HIER as DELEG, 
                '00000000', '00000000', 
                NOM, 'HIE' as TYPEDEL
            FROM HASTUTIL with(nolock)
            JOIN UO with(nolock) 
                ON UO.CODE_UO = HASTUTIL.CODE_UO
                AND UO.RESP_HIER = :user_2
            WHERE (SIG_DA = 0x54 or SIG_CDE = 0x54 or SIG_BAP = 0x54 or SIG_REF = 0x54)";
            $param += [
                ':user_2' => $_SESSION['Session_Login'],
            ];
        }

        return "SELECT 
            main.*, 
            (
                select count(*)
                from WFT_SIG with (nolock) 
                left join WF_GRP_SIG_LIST grp with(nolock) 
                    on grp.WF_GS_ENT_TYPE = WFT_SIG.TYPE_ENT 
                    and grp.WF_GS_CODE = WFT_SIG.GROUPE_SIG
                where TYPE_ENT = :module 
                    and (
                        WFT_SIG.SIGNAT = main.SIGNAT
                        or grp.WF_GS_CODE_SIG = main.SIGNAT
                    ) and SIGNABLE = 0x54
            ) nbSign 
            FROM ("
            . implode(' UNION ', $listReq)
            . ') as main
            WHERE 1=1 ';
    }

    public function countDelegationRecue(string $module) :int
    {
        $arrayReq = [];
        return intval($this->fetchAssocOne(
            "SELECT SUM(nbSign) as count
            FROM ("
            . $this->listeDelegationRecueBaseRequest($module, $arrayReq)
            . ") as listDeleg",
            $arrayReq
        )['count'] ?? 0);
    }
    
    /**
     * listeFournisseurByCode
     * @return array<mixed>
     */
    public function listeFournisseurByCode() : ?array
    {
        $req =" SELECT * FROM WFT_SIG with (nolock) where TYPE_ENT = 'FOURNISS' and SIGNAT = '".$_SESSION['Session_Login']."'";
        
        $incident = $this->pdo->prepare($req);
        $incident->execute();
        
        return $incident->fetchAll($this->pdo::FETCH_ASSOC);
    }
}
