<?php

namespace Modules\GlobalApplication\Repository;

use Helpers\Outil\Sql;
use Helpers\QueryBuilder\AbsRepository;

class UO extends AbsRepository
{
    
    /**
     * listeUOUser
     * @param string $user
     * @param string $societe
     * @return array<mixed>
     */
    public function listeUOUser(string $user, string $societe) : ?array
    {
        $req = "select * from LSTUOPARUSR with (nolock) where CODE_USER=:user";
        
        $arrayReq = [':user' => $user];
        
        if (Sql::boolSql(Sql::paravrac($this->pdo, 'UOSECTO', 'PARAMSTE'))) {
            $req .=" and CODE_STE = :societe";
            $arrayReq[':societe'] = $societe;
        } else {
            $req .=" and CODE_STE = ''";
        }
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    

    /**
     * listeUoRespHierarchique
     * @param string $user
     * @param string $societe
     * @return array<mixed>
     */
    public function listeUoRespHierarchique(string $user, string $societe) : ?array
    {
        $req = "select * from UO with (nolock) where RESP_HIER=:user";
        $arrayReq = [':user' => $user];
        
        if (Sql::boolSql(Sql::paravrac($this->pdo, 'PARAMSTE', 'UOSECTO'))) {
            $req .=" and CODE_STE = :societe";
            $arrayReq[':societe'] = $societe;
        } else {
            $req .=" and UO_CODE_SOCIETE = ''";
        }
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
  
    /**
     * uOFille
     * @param string $uo
     * @return array<mixed>
     */
    public function uOFille($uo) : ?array
    {
        $req = "select * from UO with (nolock) where UO_MERE=:uo";
        
        $arrayReq = [':uo' => $uo];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    

    /**
     * listeSI
     * @param array<string> $listeUO
     * @param string $societe
     * @return array<mixed>
     */
    public function listeSI(array $listeUO, string $societe) : ?array
    {
        $req = "select * from LSTSIPARUO with (nolock) where CODE_UO IN ('".implode("','", $listeUO)."')";
        
        if (\Helpers\Outil\Roles::droit($this->pdo, 'SISECTOR', 'PARAMSTE')) {
            $req .= "  and CODE_STESI=:societe";
            $arrayReq[':societe'] = $societe;
        } else {
            $req .= "  and CODE_STESI=''";
            $arrayReq = [];
        }
        
        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * @return array<mixed>
     */
    public function getListeUOUser()
    {
        $req = "exec HASSTD_ListeUO_PerimetreUser 0,'','',:user,0";
        $arrayReq = [':user' => $_SESSION["Session_Login"]];

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