<?php

namespace Entity\Model\Alias;

use Entity\Model\Paravrac;
use Helpers\Outil\ArrayHelper;
use Helpers\Outil\Sql;

class ParamFourniss extends Paravrac
{

    public static string $FouTaxeType = 'FouTaxType';
    
    /**
     * @_return array<string|int,array<object{
     *     label:string,
     *     name:string,
     *     validator:array<mixed>
     * }>> Map of list of field indexed by Pays id
     * @return array<string|int,array<\stdClass>> Map of list of field indexed by Pays id
     */
    public function getTaxeFieldMap() :array
    {
        $paramBloquant = Sql::paravrac($this->pdo, 'PARAMFOU', 'CTL_TAXE');
        list ($obli['NO_SIRET'], $obli['TAXE2'], $obli['TAXE3'], $obli['TAXE4']) = explode('|', $paramBloquant);

        // TODO : MEMOIZE ?

        $mapParam = $this->getMap('PARAMFOUUF');
        $mapTaxeType = $this->getMap(ParamFourniss::$FouTaxeType);



        $tmp = [];
        foreach ($mapParam as $pays => $paravrac) {
            $paravrac = explode("|", $paravrac);
            $listField = [];
            for ($i = 0; $i < count($paravrac); $i+=2) {
                $vali = explode(";", $paravrac[$i + 1] ?? '0;N');
                $name = [
                    0 => 'NO_SIRET',
                    2 => 'TAXE2',
                    4 => 'TAXE3',
                    6 => 'TAXE4',
                ][ $i ];

                $avalidator = [
                    'max' => intval($vali[0]),
                    'mandatory' => $obli[$name] == 'B'? true:false,
                ];

                if ($pays == 'FR' && $name=='NO_SIRET') {
                    $avalidator['mandatoryPRO'] = sql::paravrac($this->pdo, 'PARAMFOU', 'PRO_SAISIE_SIRET') == 'B'? true:false;
                    $avalidator['mandatoryFOU'] = sql::paravrac($this->pdo, 'PARAMFOU', 'FOU_SAISIE_SIRET') == 'B'? true:false;
                }

                $listField[ $name ] = (object) [
                    'label' => $paravrac[$i],
                    'name' => $name,
                    'validator' => $avalidator,
                    // 'validator' => function (Validator $validator, FormInput $input, $value, $root) use ($vali) {
                    //     $validator->isStringSansEspace($input->getLabel(), $value, 0, intval($vali[0]));// TODO : $vali[1]
                    // }
                    'type' => $mapTaxeType[ $pays . '_' . $name ] ?? null,
                ];
            }
            $tmp[ $pays ] = $listField;
        }
        
        return $tmp
            // Default config if none exist
            + [
                'FR' => $this->getDefaultParamFR(),
                '_default' => $this->getDefaultParam()
            ];
    }

    /**
     * @return array<mixed>
     */
    public function getTaxeField(string $codePays) :array
    {
        $configTaxe = $this->getTaxeFieldMap();
        return $configTaxe[ $codePays ]
            ?? $configTaxe[ '_default' ]
        ;
    }

    /**
     * @return array<\stdClass>
     */
    public function getDefaultParam() :array
    {
        return [
            'NO_SIRET' => (object) [
                'label' => '',
                'name' => 'NO_SIRET',
                'validator' => ['max' => 14,'mandatory' => false],
                'type' => null,
            ],
            'TAXE2' => (object) [
                'label' => '',
                'name' => 'TAXE2',
                'validator' => ['max' => 14, 'mandatory' => false],
                'type' => null,
            ],
            'TAXE3' => (object) [
                'label' => '',
                'name' => 'TAXE3',
                'validator' => ['max' => 14, 'mandatory' => false],
                'type' => null,
            ],
            'TAXE4' => (object) [
                'label' => '',
                'name' => 'TAXE4',
                'validator' => ['max' => 14, 'mandatory' => false],
                'type' => null,
            ],
        ];
    }

    /**
     * @return array<\stdClass>
     */
    public function getDefaultParamFR() :array
    {
        return [
            'NO_SIRET' => (object) [
                'label' => 'N° SIRET',
                'name' => 'NO_SIRET',
                'validator' => ['max' => 14,'mandatory' => false],
                'type' => 'SIRET',
            ],
        ] + $this->getDefaultParam();
    }

    public function getSiretControl(bool $isProspect = false) :?string
    {
        return $this->getValue(
            'PARAMFOU',
            ($isProspect ? 'PRO' : 'FOU') . '_SAISIE_SIRET'
        );
    }

    public function getSiretControlUniq(bool $isProspect = false) :?string
    {
        return $this->getValue(
            'PARAMFOU',
            ($isProspect ? 'PRO' : 'FOU') . '_CTL_UNI_SIRET'
        );
    }
    
    /**
     * @return null|\stdClass
     */
    public function getSirenField()
    {
        return ArrayHelper::findFirst(
            $this->getTaxeField('FR'),
            fn($fieldInfo) => $fieldInfo->type == 'SIREN'
        );
    }
}
