<?php

namespace Modules\Utilisateur\Repository;

use Entity\Model\OxaUserCnt;
use Helpers\QueryBuilder\AbsRepository;

class MaSession extends AbsRepository
{
    /**
     * @param array<mixed> $listeUser
     * @return array<mixed>
     */
    public function getAllDroit($listeUser)
    {
        $select  = '';
        $array = [];
        $i = 0;
        foreach ($listeUser as $user) {
            $select .= '(select case when exists
                            (select COD_DR from DROITUSR with (nolock) where COD_USR=:user1_'.$i.' and COD_DR=DROITS.COD_DR
                            union
                            select dg.COD_GRP from DROITGRP dg with (nolock)
                            inner join GRPUSR g with (nolock) on g.COD_GRP=dg.COD_GRP and g.COD_USR=:user2_'.$i.'
                            where dg.COD_DR=DROITS.COD_DR)
                            then 1
                            else 0
                            end as DROIT
                        ) valeur'.$i.',';

            $array[':user1_'.$i] = $user;
            $array[':user2_'.$i] = $user;

            $i++;
        }

        $req = "SELECT $select
                    DROITS.COD_DR, 
                    {$this->selectTrad('DROITS.LIB_DR')},
                    {$this->selectTrad('DROITS.LIBC_DR', 'LIBC_DR', 'TRAD2')},
                    DROITS.CAT_DR, 
                    DROITS.NUM_CLT, 
                    DROITS.PUB, 
                    DROITS.NOUVEAU 
                FROM (
                    SELECT
                        ROW_NUMBER() OVER(
                        ORDER BY (
                        SELECT
                            1)) ORDRE,
                            x.*
                    FROM
                        DROITS x with (nolock)) DROITS
                    {$this->joinTrad('DROITS.LIB_DR', $array)}
                    {$this->joinTrad('DROITS.LIBC_DR', $array, 'TRAD2')}
                WHERE (NUM_CLT = '0' OR NUM_CLT = :numClient)
                ORDER BY DROITS.ORDRE";

        $array[':numClient'] = $_SESSION['Session_NumClient'];

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

    private function getCodeType(string $code) :string
    {
        if ($code == 'INIT_IMPUT' || $code == 'AUTOCOMP_TPS_LATENCE' || $code == 'AUTOCOMP_NB_CARACT_MIN' || $code == 'AUTOCOMP_NB_RESULT_MAX') {
            $CodeType = 'SESSION';
        } elseif ($code == 'NB_ART_PAGE_RECH' || $code == 'AUTOCOMPLETION' || $code == 'TRI_ARTICLE') {
            $CodeType = 'RECHERCHE';
        } else {
            return 'PROFIL';
        }

        return $CodeType;
    }
    public function updateValueUser(string $code, string $valeur) : void
    {
        $oxaUserCnt = new OxaUserCnt($this->pdo);

        $find = $oxaUserCnt->find(['COD_USR' => $_SESSION["Session_Login"], 'COD_CHAMP' => $code]);

        if ($find) {
            $oxaUserCnt->update([
                'VALEUR' => $valeur
            ], [
                'COD_USR' => $_SESSION["Session_Login"],
                'COD_CHAMP' => $code
            ]);
        } else {
            $oxaUserCnt->insert([
                'VALEUR' => $valeur,
                'COD_TYPE' => $this->getCodeType($code),
                'COD_USR' => $_SESSION["Session_Login"],
                'COD_CHAMP' => $code
            ]);
        }
    }

    public function getDefautSession(string $code) : string
    {
        $oxaUserCnt = new OxaUserCnt($this->pdo);

        $find = $oxaUserCnt->find(['COD_USR' => $_SESSION["Session_Login"], 'COD_CHAMP' => $code]);

        if ($find) {
            $value = $find['VALEUR'];
        } else {
            if ($code == 'INIT_IMPUT') {
                $value = 'INITIALISER';
            } elseif ($code == 'AUTOCOMPLETION') {
                $value = '1';
            } elseif ($code == 'AUTOCOMP_NB_CARACT_MIN') {
                $value = '0';
            } elseif ($code == 'AUTOCOMP_TPS_LATENCE') {
                $value = '0';
            } elseif ($code == 'AUTOCOMP_NB_RESULT_MAX') {
                $value = '20';
            } elseif ($code == 'NB_ART_PAGE_RECH') {
                $value = '30';
            } else {
                $value = '';
            }

            $oxaUserCnt->insert([
                'VALEUR' => $value,
                'COD_TYPE' => $this->getCodeType($code),
                'COD_USR' => $_SESSION["Session_Login"],
                'COD_CHAMP' => $code
            ]);
        }
        return $value;
    }

    /**
     * @return array<mixed>
     */
    public function getParamSocieteUser(string $societe, ?string $codeUser = null)
    {
        $req = "SELECT * from (
            select CODE_STE, SI, CDC, SCA, CTRLIV, CTR_ADR, SIT_STO 
            from HASTUTIL with (nolock) where CODE = :user
            
            union 

            select COD_STE CODE_STE, CODSI SI, CODCDC CDC, CODSCA SCA, CTRLIVR, CTREMET CTR_ADR, CODSIT SIT_STO
            from SOC_USR  with(nolock) where COD_USR = :user2
        ) socUser where CODE_STE = :societe";

        if (!$codeUser) {
            $codeUser = $_SESSION["Session_Login"];
        }

        $array= [
            ':user' => $codeUser,
            ':user2' => $codeUser,
            
            ':societe' => $societe,
        ];

        $requete = $this->pdo->prepare($req);
        $requete->execute($array);
        return $requete->fetch($this->pdo::FETCH_ASSOC);
    }
}
