<?php

namespace Modules\Consultation\Rules;

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

class SaisieReponses
{
    /** @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;

    /**
     * @param ServerRequestInterface $request
     * @param \PDO $pdo
     */
    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_SRF' => Roles::droitsUtilisateur($this->pdo, 'HA_AO_SRF'),
        ];
    }

    /**
     * @return bool
     */
    public function createEditEntet(): bool
    {
        $idAO       = $this->request->getAttribute('idAO');
        $typeFG     = $this->request->getAttribute('typeFG');
        $idFrs      = $this->request->getAttribute('idFrs');
        $idRep      = $this->request->getAttribute('idRep') ?? '';
        $CV         = $this->request->getAttribute('CV') ?? '';

        $aoEntet        = new AoEntet($this->pdo);
        $aoDaRep        = new AoDaRep($this->pdo);
        $consultation   = $aoEntet->find($idAO, [], [], $this->pdo::FETCH_ASSOC);
        $fournisseur    = $this->fournissEntity->find($idFrs, [], [], $this->pdo::FETCH_ASSOC);

        $infoEnteteRep  = $aoDaRep->find([
            'NO_AO'     => $idAO,
            'CODE_FOU'  => $idFrs,
            'DTE_REP'   => $idRep,
            'C_V'       => $CV,
            'TYPE_FG'   => $typeFG,
        ]);
        
        $canEdit    = ($infoEnteteRep['EVE_REP'] ?? '0') == '0';
        $enCours    = ($consultation['DTE_VALID_AO'] != '00000000');

        if (!$canEdit && $CV !== '') {
            return false;
        }

        if (!$enCours
            && (
                (Sql::toBool($fournisseur['GEREPORTAIL']) ||
                $fournisseur['ctcOxalinkActif'] !== 0) ||
                !$this->droits['HA_AO_SRF']
            ) && (
                (! Sql::toBool($consultation['ACC_CONF']) && ! Sql::toBool($fournisseur['ACC_CONF_VALID']))
                || Sql::toBool($consultation['ACC_CONF'])
            )
        ) {
            return false;
        }

        return true;
    }

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

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

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

        $consultation = $this->consultationEntity->find($idAO, [], [], $this->pdo::FETCH_ASSOC);

        if ($consultation['RFI_Q'] !== 'Q') {
            return false;
        }

        return true;
    }

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

        if ($CV !== 'V') {
            return false;
        }

        return true;
    }

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


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

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

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

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