<?php
namespace Modules\Index\Repository;

use Helpers\Outil\Roles;
use Helpers\QueryBuilder\AbsRepository;

class User extends AbsRepository
{
    public $isAdmin = false;
    // Utilisateur est-il admin?
    public $authUser = false;
    // Authentification réussie = true
    
    /**
     * getIsAdmin
     * @param string $login
     * @param string $mdp
     */
    public function getIsAdmin(String $login, String $mdp)
    {
        $req =
        "SELECT *
        FROM SV_PARAMETRESGENERAUX with (nolock)
        WHERE UPPER(ADMINISTRATEUR) = :login AND MDP_ADMIN = :mdp";
        $arrayReq = [':login' => strtoupper($login), ':mdp' => $mdp];
        $user = $this->pdo->prepare($req);
        $user->execute($arrayReq);
        if ($user->fetchAll()) {
            $this->isAdmin = true;
            $_SESSION['UTILISATEUR'] = 'Admin';
        }
        return $this->isAdmin;
    }
    
    public function getIsUser($login)
    {
        $req =
        "SELECT *
        FROM SV_UTILISATEURS with (nolock)
        WHERE UPPER(CODE_UTILISATEUR) = :login";
        $arrayReq = [':login' => strtoupper($login)];
        $user = $this->pdo->prepare($req);
        $user->execute($arrayReq);
        $user = $user->fetchAll();
        if (isset($user[0])) {
            return true;
        }
        return false;
    }
    
    public function authUser($login, $mdp)
    {
        $req =
        "SELECT * 
        FROM SV_UTILISATEURS with (nolock)
        WHERE UPPER(CODE_UTILISATEUR) = :login AND MOTDEPASSE = :mdp and ACTIF = 1";
        $arrayReq = [':login' => strtoupper($login), ':mdp' => $mdp];
        $user = $this->pdo->prepare($req);
        $user->execute($arrayReq);
        $user = $user->fetchAll();
        if (isset($user[0])) {
            $this->authUser = true;
            $_SESSION['UTILISATEUR'] = $user[0]['NOM_UTILISATEUR'];
            $_SESSION['CODE_UTILISATEUR'] = $user[0]['CODE_UTILISATEUR'];
        }
        return $this->authUser;
    }
    
    /**
     * getDroitUser
     * @param string $login
     * @return bool
     */
    public function getDroitUser(String $login) : Bool
    {
        $req =
        "SELECT SV_DROITSUTILISATEURS.*
        FROM SV_UTILISATEURS as SV_UTILISATEURS with (nolock) 
        INNER JOIN SV_DROITSUTILISATEURS as SV_DROITSUTILISATEURS with (nolock) ON V_UTILISATEURS.CODE_UTILISATEUR=SV_DROITSUTILISATEURS.CODE_UTILISATEUR
        WHERE UPPER(SV_UTILISATEURS.CODE_UTILISATEUR) = :login";
        $arrayReq = [':login' => strtoupper($login)];
        $user = $this->pdo->prepare($req);
        $user->execute($arrayReq);
        $listeDroits = $user->fetchAll();
        foreach ($listeDroits as $lgDroit) {
            $_SESSION['DROITS_UTILISATEUR'][str_replace(' ', '', $lgDroit['CODE_DROIT'])] = 1;
        }
        return true;
    }


    /**
     * rqGetTypeBackupForUser
     * @param string $user
     * @param string $backup
     */
    public function rqGetTypeBackupForUser(String $user, String $backup)
    {
        $req = ' select * from USRBACK with(nolock) where COD_BAC = :COD_BAC and COD_USR = :COD_USR';
        $arrayReq= [
            // @phpstan-ignore-next-line
            ':COD_BAC' => $backup,
            // @phpstan-ignore-next-line
            ':COD_USR' => $user
        ];

        $user = $this->pdo->prepare($req);
        $user->execute($arrayReq);
        return $user->fetch();
    }

    

    /**
     * droitsUtilisateur
     * @param string $droit
     * @return array<string>
     */
    public function droitsUtilisateur(String $droit, $ident = null) : ?array
    {
        $user = $_SESSION['Session_Login'] ?? $ident;
        $arrayReq = [];

        if ($user == 'OXAUSER') {
            $req="select 1 as  DROIT";
        } else {
            $req= "SELECT case when exists
                (select COD_DR from DROITUSR with(nolock) where COD_USR=:COD_USR and COD_DR=:COD_DR
                union
                select dg.COD_GRP from DROITGRP dg with(nolock)
                join GRPUSR g with(nolock) on g.COD_GRP=dg.COD_GRP and g.COD_USR=:COD_USR1
                where dg.COD_DR=:COD_DR1)
                then 1
                else 0
                end as DROIT";
                $arrayReq[':COD_DR'] = $droit;
                $arrayReq[':COD_DR1'] = $droit;
                $arrayReq[':COD_USR'] = $user;
                $arrayReq[':COD_USR1'] = $user;
        }

        
        $user = $this->pdo->prepare($req);
        $user->execute($arrayReq);
        return $user->fetch();
    }

    /**
     * @return array<mixed>
     */
    public function getSocieteOuverteMulti()
    {
        $req = "select TOP 1 SOCIETE.CODE_STE from SOCIETE with (nolock) where 1=1 ".Roles::getClauseSocieteOuvert('SOCIETE');

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

    /**
     * @return array<mixed>
     */
    public function getSocieteOuvertePluri(string $codeUser)
    {
        $arrayReq[':user'] = $codeUser;

        $req = "select TOP 1 SOCIETE.CODE_STE from SOC_USR with (nolock) 
                left join SOCIETE with (nolock) on SOCIETE.CODE_STE = SOC_USR.COD_STE 
                where SOC_USR.COD_USR = :user ".Roles::getClauseSocieteOuvert('SOCIETE') ;

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