<?php

namespace Modules\Stock\Validator;

use Entity\Model\Paravrac;
use Entity\Model\SSiteSto;
use Exception;
use Helpers\Trad\ITrad;
use Helpers\Validator\Validator;
use Modules\Ajax\Repository\Listes;
use Modules\Stock\Trait\Validator\LigneValidatorTrait;
use Modules\Stock\Trait\Validator\ValidatorTrait;
use PDO;

class SiteStockValidator extends Validator
{
    use ValidatorTrait;
    use LigneValidatorTrait;

    private readonly Listes $listes;
    /** @phpstan-ignore-next-line */
    private readonly Paravrac $paravrac;

    public function __construct(PDO $pdo, ITrad $trad)
    {
        parent::__construct($pdo, $trad);
        $this->listes = new Listes($pdo);
        $this->paravrac = new Paravrac($pdo);
    }

    /**
     * @param array<string,mixed> $siteStock
     * @return $this
     * @throws Exception
     */
    public function validateSite(array $siteStock): self
    {
        $nomChamp = $this->trad->t('Site');
        $this->isCode($nomChamp, $siteStock['SITE'], 1, 7);

        if ((new SSiteSto($this->pdo))->find($siteStock['SITE'])) {
            $this->setError('Le code site "' . $siteStock['SITE'] . '" est déjà utilisé');
        }

        return $this;
    }

    /**
     * @param array<string,mixed> $siteStock
     * @return $this
     */
    public function validateDesign(array $siteStock): self
    {
        $this->isStringAvecEspace($this->trad->t('Désignation'), $siteStock['DESIGN'], 1, 50);

        return $this;
    }

    /**
     * @param array<string,mixed> $siteStock
     * @return $this
     */
    public function validateCodeSte(array $siteStock): self
    {
        $this->zoom(
            'Societe',
            $siteStock['CODE_STE'],
            $this->trad->t('Société'),
            $this->pdo,
            false,
            ['filter_egal_CODE_STE' => $siteStock['CODE_STE']]
        );

        return $this;
    }
}
