<?php
namespace Modules\Index\Repository;

class User
{
    private $pdo;
    public $isAdmin = false;
    // Utilisateur est-il admin ?
    public $authUser = false;
    // Authentification réussie = true
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    /**
     * 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' => ReplaceSql(strtoupper($backup)),
            // @phpstan-ignore-next-line
            ':COD_USR' => ReplaceSql(strtoupper($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();
    }
}
