<?php

namespace Modules\Contrat\Rules;

use Entity\Model\Acccadre;
use Helpers\Outil\Roles;
use Psr\Http\Message\ServerRequestInterface;

class Main
{
    protected ServerRequestInterface $request;
    protected \PDO $pdo;
    protected \Entity\Model\Acccadre $accCadreEntity;
    /**
     * @var array<mixed>
     */
    protected array $droits;
        
    /**
     * __construct
     *
     * @param  ServerRequestInterface $request
     * @return void
     */
    public function __construct(ServerRequestInterface  $request, \PDO $pdo)
    {
        $this->request  = $request;
        $this->pdo      = $pdo;
        $droits = [
            'HA_ACD_CM'   => Roles::droitsUtilisateur($this->pdo, 'HA_ACD_CM', $_SESSION["Session_Login"]), // Droit de création et modification des accords-cadres
            'HA_ACD_ADM'  => Roles::droitsUtilisateur($this->pdo, 'HA_ACD_ADM', $_SESSION["Session_Login"]), // Droit de création et modification des accords-cadres administrateur
            'HA_MAR_CM'   => Roles::droitsUtilisateur($this->pdo, 'HA_MAR_CM', $_SESSION["Session_Login"]), // Droit de création et modification des marchés
            'HA_TYM_CM'   => Roles::droitsUtilisateur($this->pdo, 'HA_TYM_CM', $_SESSION["Session_Login"]), // Droit de suppression des campagnes d'évaluation
        ];

        $this->droits = $droits;
        $this->accCadreEntity = new Acccadre($this->pdo);
    }
    
    /**
     * init
     *
     * @return bool
     */
    public function init() : bool
    {
        return ($this->droits['HA_ACD_CM'] || $this->droits['HA_MAR_CM'] || $this->droits['HA_TYM_CM'] || $this->droits['HA_ACD_ADM']);
    }

    public function contractCreatEditAccess(): bool
    {
        return $this->droits['HA_ACD_CM'] || $this->droits['HA_ACD_ADM'];
    }

    public function marketCreatEditAccess(): bool
    {
        return $this->droits['HA_MAR_CM'] || $this->droits['HA_ACD_ADM'];
    }

    public function marketTypeCreatEditAccess(): bool
    {
        return $this->droits['HA_TYM_CM'] || $this->droits['HA_ACD_ADM'];
    }

    public function contractCreatEditAccessCreator(string $contractId): bool
    {
        $contract  = $this->accCadreEntity->find($contractId);
        return (($contract['ACH_RESP'] == $_SESSION['Session_Login'] && $this->droits['HA_ACD_CM']) || ($contract['CREERPAR'] == $_SESSION['Session_Login'] && $this->droits['HA_ACD_CM']) || $this->droits['HA_ACD_ADM']);
    }
}
