<?php

namespace Modules\Suivi\Repository;

use Helpers\QueryBuilder\AbsRepository;
use Entity\Model\Hastutil;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\QueryBuilder\AbsRepositoryExport;
use Helpers\Trad\ITrad;
use Modules\Suivi\Service\RuptureService;
use Skeleton\Router;

class SuiviRepo extends AbsRepositoryExport
{
    /**
     * @var array<mixed> $parametrage
     */
    public $parametrage;

    /**
     * @var array<mixed> $user
     */
    protected $user;

    /**
     * @var array<mixed> $droit
     */
    protected $droit;

    protected router $router; // TODO : remove
    protected ITrad $trad; // TODO : remove
    protected RuptureService $rupture;
    
    public function __construct(\PDO $pdo, Router $router, ITrad $trad)
    {
        parent::__construct($pdo);

        $this->router = $router;
        $this->trad = $trad;

        $this->rupture = new RuptureService($this->pdo);

        $this->parametrage = [
            'sisecto' => \Helpers\Outil\Roles::droit($this->pdo, 'SISECTOR', 'PARAMSTE'),
            'scaSecto' => \Helpers\Outil\Roles::droit($this->pdo, 'SCASECTO', 'PARAMSTE'),
            'cdcSecto' => \Helpers\Outil\Roles::droit($this->pdo, 'CDCSECTO', 'PARAMSTE'),
            'uoSecto' => \Helpers\Outil\Roles::droit($this->pdo, 'UOSECTO', 'PARAMSTE'),

            'SOURCE_SI' => Sql::paravrac($this->pdo, 'OXAPROC', 'SOURCE_SI'),
            'SUIVI_EXCLURE_LIENDOC_UO_HASTUTIL' => Sql::paravrac($this->pdo, 'OXAPROC', 'SUIVI_EXCLURE_LIENDOC_UO_HASTUTIL'),
        ];

        $user = new Hastutil($this->pdo);
        $this->user = $user->find($_SESSION["Session_Login"]);
    }

    /**
     * use this requete with "select CodeUO from LstUO"
     *
     * @param string $user
     * @return string
     */
    private static function getPerimetreUO(string $user) :string
    {
        $req = "with LstUO(CodeUO,UOMere) as
        (
			SELECT CODE_UO,UO_MERE FROM UO with(nolock) WHERE RESP_HIER = '$user'
			UNION 
			SELECT h.CODE_UO,uom.UO_MERE FROM HASTUTIL h with(nolock)
			inner join UO uom with(nolock) on uom.CODE_UO=h.CODE_UO
			WHERE h.CODE = '$user'
			UNION
			SELECT l.CODE_UO,uom2.UO_MERE FROM LSTUOPARUSR l with(nolock)
			inner join UO uom2 with(nolock) on uom2.CODE_UO=l.CODE_UO
			WHERE l.CODE_USER = '$user'
            union all
            select u2.CODE_UO,u2.UO_MERE
            from UO u2 with(nolock)
            join LstUO l on u2.UO_MERE=l.CodeUO
        ) ";

        return $req;
    }
    
    private static function getFiltreUO(string $uo) :string
    {
        // TODO : prevent injection
        $req = "with LstUO (CodeUO) as
        (
            SELECT CODE_UO from UO with(nolock) where CODE_UO = '$uo'
            union all
            SELECT nplus1.CODE_UO from UO nplus1 with(nolock) , LstUO where LstUO.CodeUO = nplus1.UO_MERE
        )";

        return $req;
    }

    public static function getClauseUO(string $user, string $uo = ''):string
    {
        if ($uo) {
            return self::getFiltreUO($uo);
        } else {
            return self::getPerimetreUO($user);
        }
    }

    /**
     * @return array<mixed>
     */
    public function isCdeFromDAoxaproc(string $noCde)
    {
        $req = "SELECT 
                count(DA_ENTET.TYP_DOC) NB_DA_FOR_CDE
                from DA_ENTET with(nolock)
                where DA_ENTET.NO_DA in (
                    select distinct CA_LIGNE.NO_DA 
                    from CA_LIGNE with(nolock) 
                    where CA_LIGNE.NO_CDE = :noCde)";

        $arrayReq = [':noCde' => $noCde];

        return $this->fetchAssocOne($req, $arrayReq);
    }

   /**
     * @return array<mixed>
     */
    public function isDAFromOxaproc(string $numDa)
    {
        $req = "select 
                    case	
                        when TYP_DOC = (select LIB01PARV from PARAVRAC with (nolock) where TYPEPARV='OXAPROC' and CODEPARV='TYPDOCUMENT' ) 
                        then 1 else 0 
                    end IS_DA_OXAPROC
                from DA_ENTET WITH(NOLOCK)
                where DA_ENTET.NO_DA = :noDa";
        
        $arrayReq = [':noDa' => $numDa];

        return $this->fetchAssocOne($req, $arrayReq);
    }
}
