<?php

namespace Modules\Stock\Trait\Validator;

use Entity\Model\Codgest;
use Entity\Model\CsDon;
use Entity\Model\Sectimpu;
use Exception;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Modules\Ajax\Repository\Listes;
use Modules\GlobalApplication\Repository\LicenceOxalys;
use Modules\Stock\Service\ParametrageService;

trait ValidatorTrait
{
    /**
     * @param array<string,mixed> $enteEntity
     * @return $this
     */
    public function validateDateCreation(array &$enteEntity, string $property = 'DTE_CREAT'): self
    {
        $this->isDate($enteEntity[$property]);
        $this->isDateGreaterThanToday($enteEntity[$property], $this->trad->t('Date de création'));

        return $this;
    }

    /**
     * @param string $codeTab
     * @param string|array<int,string> $codeEnr
     * @param array<string,mixed> $post
     * @param bool|null $obligExtern
     * @param bool $create
     * @return $this
     * @throws Exception
     */
    public function validateCs(string $codeTab, string|array $codeEnr, array &$post = [], ?bool $obligExtern = null, bool $create = false): self
    {
        $spliter = Constante::spliter;
        $codeEnr = ChampSupp::encodeCodeEnr($codeTab, $codeEnr);

        if ($create) {
            foreach ($post as $key => $value) {
                if (str_starts_with($key, 'CS_EXTENTION')) {
                    $needle = $codeTab.$spliter.$spliter;
                    $pos = strpos($key, $needle);
                    $numFix = substr($key, $pos + strlen($needle));
                    $newKey = 'CS_EXTENTION'.$spliter.$codeTab.$spliter.$codeEnr.$spliter.$numFix;
                    $post[$newKey] = $value;
                    unset($post[$key]);
                }
            }
        }

        if (!empty($post)) {
            $this->validatorCS($codeTab, $codeEnr, $post, $this->pdo, '', 0, '', false, $obligExtern);
        } else {
            $csDon = new CsDon($this->pdo);
            $csDonEntities = $csDon->findAll(['COD_TAB' => $codeTab, 'COD_ENR' => $codeEnr]);

            foreach ($csDonEntities as $cs) {
                $key = 'CS_EXTENTION'.$spliter.$cs['COD_TAB'].$spliter.$codeEnr.$spliter.$cs['NUM_FIX'];
                $post[$key] = $cs['VALEUR'];
            }

            $this->validatorCS($codeTab, $codeEnr, $post, $this->pdo);
        }

        return $this;
    }

    /**
     * @param array<string,mixed> $entity
     * @throws Exception
     */
    public function validateCodgest(array $entity): self
    {
        $sectimpEntity = (new Sectimpu($this->pdo))->find(['CODE_SI' => $entity['SECTIMP']]);
        if (!(new ParametrageService($this->pdo))->isCodeGestionActive() || !Sql::toBool($sectimpEntity['SICODG'])) {
            return $this;
        }

        $cgParId = Roles::droit($this->pdo, 'CODE_GESTION_PAR_ID')? 1:0;

        $arrayCG = [
            'CODE_SI' => $entity['SECTIMP'],
            'NUM_LOT' => '1',
            'CODE_STE' => $sectimpEntity['CODE_STE'],
            'cgParId' => $cgParId
        ];

        $this->codeGestionZoom($entity['CODGES'], $this->trad->t('Code gestion'), $this->pdo, !Sql::toBool($sectimpEntity['IMPCODG']), $arrayCG);
        
        return $this;
    }

    /**
     * @param string $nomChamps
     * @param string $value
     * @param string $indice
     * @param bool $obligatoire
     * @param int|null $max
     * @param int|null $min
     * @return $this
     */
    public function validatePercentage(string $nomChamps, string &$value, string $indice = '13.2', bool $obligatoire = false, ?int $max = 100, ?int $min = 0): self
    {
        $this->isFloat($nomChamps, $value, $indice, $obligatoire, $max, $min);

        if (is_string($value)) {
            $value = (float) $value;
        }

        return $this;
    }
}
