<?php

namespace Modules\GlobalApplication\Rules;

use Entity\Model\CaEntet;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Roles;
use Psr\Http\Message\ServerRequestInterface;

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

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

    /** @var string $module */
    protected string $module;

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

    /**
     * __construct
     *
     * @param  ServerRequestInterface $request
     * @return void
     */
    public function __construct(ServerRequestInterface  $request, \PDO $pdo)
    {
        $this->request  = $request;
        $this->pdo      = $pdo;
        $this->module   = $request->getAttribute('MODULE');
        $this->id       = $request->getAttribute('id');
    }

    /**
     * @return bool
     */
    public function init(): bool
    {
        if (empty($this->module)) {
            return false;
        }
        switch ($this->module) {
            case "CA":
                $commande = (new CaEntet($this->pdo))->find($this->id);
                return Roles::droitsUtilisateur($this->pdo, 'HA_CA_CMV')
                    && $commande
                    && $commande['DTE_VALID'] != '00000000'
                    && (new ChampSupp($this->pdo))->hasCommandeModApVal($this->id);
            default:
            // TODO : where are the conditions for other module ?
        }

        return true;
    }
}
