<?php

namespace Modules\Consultation\Rules;

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

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

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

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

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

    public function __construct(ServerRequestInterface $request, \PDO $pdo)
    {
        $this->request              = $request;
        $this->pdo                  = $pdo;
        $this->currentUser          = $_SESSION['Session_Login'];
        $this->consultationEntity   = new AoEntet($pdo);
    }

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

    /**
     * @return bool
     */
    public function creatEdit(): bool
    {
        if (Roles::droitsUtilisateur($this->pdo, 'HA_AO_ADM') ||
            Roles::droitsUtilisateur($this->pdo, 'HA_AO_CMV')
        ) {
            return true;
        }

        return false;
    }

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

    /**
     * @return bool
     */
    public function ajaxUpdateComp(): bool
    {
        $droits = [
            'HA_AO_ADM' => Roles::droitsUtilisateur($this->pdo, 'HA_AO_ADM'),
            'HA_AO_CMV' => Roles::droitsUtilisateur($this->pdo, 'HA_AO_CMV')
        ];

        $idAO           = $this->request->getAttribute('idAO');
        $consultation   = $this->consultationEntity->find($idAO);
        $canEdit        = ($droits['HA_AO_ADM'] || ($droits['HA_AO_CMV'] && $consultation['ACH'] == $_SESSION["Session_Login"]));

        if (!$canEdit) {
            return false;
        }

        return true;
    }

    /**
     * @return bool
     */
    public function delete(): bool
    {
        if (!Roles::droitsUtilisateur($this->pdo, 'HA_AO_SUP')) {
            return false;
        }

        return true;
    }


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

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

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

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

    /**
     * @return bool
     */
    // @phpcs:ignore
    public function ajaxUpdateEmailAuto(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    // @phpcs:ignore
    public function cloture(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    // @phpcs:ignore
    public function popupUpdateDate(): bool
    {
        return true;
    }
}
