<?php

namespace Entity\Model\Alias;

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

class ParamFourniss extends Paravrac
{
    
    /**
     * @_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 ?

        $listeParam = $this->findAll(["TYPEPARV" => 'PARAMFOUUF']);
        foreach ($listeParam as &$param) {
            if ($param['CODEPARV'] == 'FR') {
                $obli['NO_SIRET'] = [
                    'PRO' => sql::paravrac($this->pdo, 'PARAMFOU', 'PRO_SAISIE_SIRET'),
                    'FOU' => sql::paravrac($this->pdo, 'PARAMFOU', 'FOU_SAISIE_SIRET'),
                ];
            }

            $param += ['OBLI' => $obli];
        }

        return array_map(
            function ($paravrac) {
                $obli = $paravrac['OBLI'];
                
                $paravrac = explode("|", $paravrac["LIB01PARV"]);
                $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' => !is_array($obli[$name])? ($obli[$name] == 'B'? true:false):false,
                    ];

                    if ($name=='NO_SIRET' && is_array($obli[$name])) {
                        $avalidator['mandatoryPRO'] = $obli[$name]['PRO'] == 'B'? true:false;
                        $avalidator['mandatoryFOU'] = $obli[$name]['FOU'] == '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]
                        // }
                    ];
                }
                return $listField;
            },
            array_column($listeParam, null, 'CODEPARV')
        )
        // 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],
            ],
            'TAXE2' => (object) [
                'label' => '',
                'name' => 'TAXE2',
                'validator' => ['max' => 14, 'mandatory' => false],
            ],
            'TAXE3' => (object) [
                'label' => '',
                'name' => 'TAXE3',
                'validator' => ['max' => 14, 'mandatory' => false],
            ],
            'TAXE4' => (object) [
                'label' => '',
                'name' => 'TAXE4',
                'validator' => ['max' => 14, 'mandatory' => false],
            ],
        ];
    }

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