<?php

namespace Modules\DemandeAchat\Rules;

use Modules\DemandeAchat\Repository\DemandeAchat;
use Psr\Http\Message\ServerRequestInterface;

class SuppressionDemandeAchat
{
    /** @var ServerRequestInterface $request */
    private ServerRequestInterface $request;

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

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

    /** @var DemandeAchat $daRepo */
    private DemandeAchat $daRepo;

    /**
     * @param ServerRequestInterface $request
     * @param \PDO $pdo
     */
    public function __construct(ServerRequestInterface $request, \PDO $pdo)
    {
        $this->request              = $request;
        $this->pdo                  = $pdo;
        $this->droits               = [
            'DA_CreatModif' => $_SESSION['DA_CreatModif'] ?? null
        ];
        $this->daRepo               = new DemandeAchat($this->pdo);
    }

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

        if (!$idDA) {
            return false;
        }

        $da = $this->daRepo->demandeAchat($idDA);

        if (!$da) {
            return false;
        }

        if (!$this->droits['DA_CreatModif']) {
            return false;
        }

        return true;
    }
}
