<?php

namespace Modules\Stock\Validator;

use Entity\Model\Paravrac;
use Helpers\Parametres\Constante;
use Helpers\Trad\ITrad;
use Helpers\Validator\Validator;
use Modules\Ajax\Repository\Listes;
use Modules\Stock\Trait\Validator\EnteteValidatorTrait;
use Modules\Stock\Trait\Validator\LigneValidatorTrait;
use Modules\Stock\Trait\Validator\ValidatorTrait;
use PDO;

class StockLocalValidator extends Validator
{
    use ValidatorTrait;
    use EnteteValidatorTrait;
    use LigneValidatorTrait;

    /** @phpstan-ignore-next-line */
    private readonly Listes $listes;
    private readonly Paravrac $paravrac;
    public function __construct(PDO $pdo, ITrad $trad)
    {
        parent::__construct($pdo, $trad);
        $this->paravrac = new Paravrac($pdo);
        $this->listes = new Listes($pdo);
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateDelaiAppro(array $stockLocal): self
    {
        $nomChamps = $this->trad->t('Délai d\'appro');
        $this->isObligatoire($stockLocal['DELAI_APP'], $nomChamps);
        $this->isInteger($nomChamps, $stockLocal['DELAI_APP']);
        $this->isGreaterThanOrEqual($nomChamps, (float) $stockLocal['DELAI_APP'], 0);

        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateConsoPrevue(array $stockLocal): self
    {
        $this->isFloat(
            $this->trad->t('Conso. Prévue par an'),
            $stockLocal['CONSO_ANU'],
            Constante::formatQTE,
            false,
            null,
            0
        );

        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateQteEconomique(array $stockLocal): self
    {
        $this->isFloat(
            $this->trad->t('Quantité économique'),
            $stockLocal['QTE_ECO'],
            Constante::formatQTE,
            false,
            null,
            0
        );

        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateStockProtection(array $stockLocal): self
    {
        $this->isFloat(
            $this->trad->t('Stock de protection'),
            $stockLocal['STK_PRO'],
            Constante::formatQTE,
            false,
            null,
            0
        );

        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateQteMaxiStockable(array $stockLocal): self
    {
        $this->isFloat(
            $this->trad->t('Quantité Maxi stockable'),
            $stockLocal['QTE_MAX'],
            Constante::formatQTE,
            false,
            null,
            0
        );


        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateDteInv(array &$stockLocal): self
    {
        $this->isDate($stockLocal['DTE_INV'], '', '', true, $this->trad->t('Date du prochain'));

        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateEmplac(array $stockLocal): self
    {
        $this->isStringAvecEspace($this->trad->t('Emplacement'), $stockLocal['EMPLAC'], 0, 10);

        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validateDteFerm(array &$stockLocal): self
    {
        $this->isDate($stockLocal['DTE_FERM'], '', '', true, $this->trad->t('Date de fermeture'));

        return $this;
    }

    /**
     * @param array<string,mixed> $stockLocal
     * @return $this
     */
    public function validatePerioInv(array $stockLocal): self
    {
        $this->isInteger($this->trad->t('Périodicité'), $stockLocal['PERIO_INV'], 0, 365, true);

        return $this;
    }

    /**
     * @param array<string,mixed> $ligneEntity
     * @return $this
     */
    public function validateCodeArt(array $ligneEntity): self
    {
        $post = [
            'valide' => 1,
            'not_on_site' => $ligneEntity['SITE'],
            'filter_egal_SA#point#CODE_ART' => $ligneEntity['CODE_ART']
        ];
        $codArticles = $this->listes->listArticlesNotOnSite($post, false);

        $this->isObligatoire($ligneEntity['SITE'], $this->trad->t('Code article'));
        $this->contentInArray($this->trad->t('Code article'), $ligneEntity['CODE_ART'], array_column($codArticles, null, 'CODE_ART'));

        return $this;
    }
}
