<?php

namespace Modules\Consultation\Rules;

use Entity\Model\AoEntet;
use Entity\Model\Fourniss;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Psr\Http\Message\ServerRequestInterface;

class AjoutFournisseur
{
    /** @var ServerRequestInterface $request */
    protected ServerRequestInterface $request;

    /** @var \PDO $pdo */
    protected \PDO $pdo;

    /** @var string|mixed $currentUser */
    protected $currentUser;

    /** @var AoEntet $consultationEntity */
    protected AoEntet $consultationEntity;

    /** @var Fourniss $fournissEntity */
    protected Fourniss $fournissEntity;

    /** @var array<mixed> $droits */
    protected array $droits;

    public function __construct(ServerRequestInterface $request, \PDO $pdo)
    {
        $this->request              = $request;
        $this->pdo                  = $pdo;
        $this->currentUser          = $_SESSION['Session_Login'];
        $this->consultationEntity   = new AoEntet($pdo);
        $this->fournissEntity       = new Fourniss($pdo);
        $this->droits               = [
            'HA_AO_ADM' => Roles::droitsUtilisateur($this->pdo, 'HA_AO_ADM'),
            'HA_AO_CMV' => Roles::droitsUtilisateur($this->pdo, 'HA_AO_CMV')
        ];
    }

    /**
     * @return bool
     */
    public function listeFournisseur(): bool
    {
        $idAO           = $this->request->getAttribute('idAO');
        $consultation   = $this->consultationEntity->find($idAO);
        $canEditEnCours = ($this->droits['HA_AO_ADM'] || ($this->droits['HA_AO_CMV'] && $consultation['ACH'] == $this->currentUser));

        if (!$canEditEnCours) {
            return false;
        }

        return true;
    }

    public function editFournisseur(): bool
    {
        $idAO           = $this->request->getAttribute('idAO');
        $consultation   = $this->consultationEntity->find($idAO);
        $canEdit        = ($this->droits['HA_AO_ADM'] || ($this->droits['HA_AO_CMV'] && $consultation['ACH'] == $_SESSION["Session_Login"])) && $consultation['DTE_VALID_AO'] == '00000000';

        if (!$canEdit) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function deleteFournisseur(): bool
    {
        $idAO           = $this->request->getAttribute('idAO');
        $consultation   = $this->consultationEntity->find($idAO);
        $canEdit        = ($this->droits['HA_AO_ADM'] || ($this->droits['HA_AO_CMV'] && $consultation['ACH'] == $_SESSION["Session_Login"])) && $consultation['DTE_VALID_AO'] == '00000000';

        if (!$canEdit) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function ajaxUpdateAdresse(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function ajaxUpdateContact(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function popupCreateEditAccord(): bool
    {
        $idAO           = $this->request->getAttribute('idAO');
        $idFrs          = $this->request->getAttribute('idFrs');
        
        $fournisseur    = $this->fournissEntity->find($idFrs);// TODO : $fournisseur not used
        $consultation   = $this->consultationEntity->find($idAO);
        $enCours        = ($consultation['DTE_VALID_AO'] != '00000000');

        if (!$enCours || ! Sql::toBool($consultation['ACC_CONF'])) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function ajaxDeleteAccord(): bool
    {
        $idAO           = $this->request->getAttribute('idAO');
        $idFrs          = $this->request->getAttribute('idFrs');

        $fournisseur    = $this->fournissEntity->find($idFrs);
        $consultation   = $this->consultationEntity->find($idAO);
        $enCours        = ($consultation['DTE_VALID_AO'] != '00000000');

        if (! $enCours
            || ! Sql::toBool($consultation['ACC_CONF'])
            && Sql::toBool($fournisseur['ACC_CONF_VALID'])
            || $fournisseur['FIC_ACC_CONF_SIGNE'] === ''
        ) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function ajaxValiderAccord(): bool
    {
        return $this->ajaxDeleteAccord();
    }

    /**
     * @return bool
     */
    public function ajaxRefuserAccord(): bool
    {
        return $this->ajaxDeleteAccord();
    }

    /**
     * @return bool
     */
    public function popupRefuserAccord(): bool
    {
        return $this->ajaxDeleteAccord();
    }
}
