<?php

namespace Modules\Parametrage\Service;

use Entity\Model\Paravrac;
use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;
use Helpers\Validator\Validator;

class ParamFactureService
{
    /**
     * @var \PDO
     */
    private $pdo;

    private Paravrac $paravrac;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
        $this->paravrac = new Paravrac($this->pdo);
    }
    

    /**
     * @return array<\stdClass>
     */
    public function getFormParamFacture(ITrad $trad) :array
    {
   /** @phpstan-ignore-next-line */
        return array_column([
            $this->inputSwitch('DENRFACTNONMODIF'),
            $this->inputSwitch('DECHFACTNONMODIF'),
            $this->inputSwitch('LIB_COMPT_OBLG'),
            $this->inputSwitch('REFFACFOU'),
            $this->inputSwitch('FACFOU_UNIQUE'),
            $this->inputSwitch('FACDTEFOURNOB'),
            (object) [
                'type' => 'SELECT',
                'name' => 'DECHFACT',
                'listValue' => ['E','F'],
                'default' => 'E',
            ],
            (object) [
                // Label is required by Validator::isInteger
                'type' => 'DEVISE',
                'name' => 'FACTOLSOLF',
                'label' => $trad->t('Montant de tolérance sur solde financier de commande'),
                'default' => '',
            ],
            $this->inputSwitch('FACSELCMDR'),
            $this->inputSwitch('ACTIVE_PREAFFFAC'),
            $this->inputSwitch('FAC_SANS_ECRAN_CTRL_CDP_MDP'),
            (object) [
                'type' => 'SELECT',
                'name' => 'FA_AUTOVALID_HORS_ECART',
                'listValue' => ['','P','E'],
                'default' => '',
            ],

            //Mails à la comptabilité - Envoyer un mail dans les cas suivants :
            $this->inputSwitch('FA_ENVOI_MAIL_COMPTA_CDE_REG'),
            $this->inputSwitch('FA_ENVOI_MAIL_COMPTA_FAC_LITIGE'),
            $this->inputSwitch('FA_ENVOI_MAIL_COMPTA_FAC_PREAFF'),
            (object) [
                // TODO : validate email
                'type' => 'TEXT',
                'name' => 'FA_MAIL_COMPTA',
                'default' => '',
            ],
            (object) [
                'type' => 'SELECT',
                'name' => 'PREAFF_MAIL',
                'listValue' => ['','D','L'],
                'default' => '',
            ],
            (object) [
                'type' => 'SELECT',
                'name' => 'sign_bap',
                'listValue' => ['R','A','T'],
                'default' => 'R',
                'listField' => [
                    'BAP_SI_PREAFF',
                    'BAP_SI_PREAFF_UNIQRECAUTO',
                ],
                'denormalize' => function (array $appParam) {
                    //                      R   A   T
                    // BAP_SI_PREAFF        0   1   1
                    // BAP_SI_..._UAUTO     0   1   0
                    if (($appParam['BAP_SI_PREAFF_UNIQRECAUTO']??'') == 'O') {
                        return 'A';
                    }
                    if (($appParam['BAP_SI_PREAFF']??'') == 'O') {
                        return 'T';
                    }
                    return 'R';
                },
                'normalize' => function (?string $value) {
                    if ($value === null) {
                        return [
                            'BAP_SI_PREAFF' => $this->getFromParavrac(
                                'BAP_SI_PREAFF',
                                'N'
                            ),
                            'BAP_SI_PREAFF_UNIQRECAUTO' => $this->getFromParavrac(
                                'BAP_SI_PREAFF_UNIQRECAUTO',
                                'N'
                            ),
                        ];
                    }
                    //                      R   A   T
                    // BAP_SI_PREAFF        0   1   1
                    // BAP_SI_..._UAUTO     0   1   0
                    switch ($value) {
                        default:
                        case 'R':
                            $BAP_SI_PREAFF = 'N';
                            $BAP_SI_PREAFF_UNIQRECAUTO = 'N';
                            break;
                        case 'A':
                            $BAP_SI_PREAFF = 'O';
                            $BAP_SI_PREAFF_UNIQRECAUTO = 'O';
                            break;
                        case 'T':
                            $BAP_SI_PREAFF = 'O';
                            $BAP_SI_PREAFF_UNIQRECAUTO = 'N';
                            break;
                    }

                    return [
                        'BAP_SI_PREAFF' => $BAP_SI_PREAFF,
                        'BAP_SI_PREAFF_UNIQRECAUTO' => $BAP_SI_PREAFF_UNIQRECAUTO,
                    ];
                },
            ],
            (object) [
                // Label is required by Validator::isInteger
                'type' => 'DEVISE',
                'name' => 'FAC_RELACH_SUR_LITIGE_NB_JOURS',
                'label' => $trad->t('Nbre de jours avant relance acheteurs'),
                'default' => '',
            ],
            $this->inputSwitch('PROFIL_TVA_ACTIF'),
            (object) [
                'type' => 'ZOOM',
                'name' => 'DEFAULT_PROFIL_TVA',
                'entity' => 'ProfilTva',
                'label' => $trad->t('Code profil de TVA par défaut'),
                'default' => '',
            ],
        ], null, 'name');
    }

    /**
     * @return object{type:string,name:string,default:bool}
     */
    public function inputSwitch(string $name, bool $default = false) :object
    {
        return (object) [
            'type' => 'SWITCH',
            'name' => $name,
            'default' => $default ? 'O' : 'N',
        ];
    }

    /**
     * @param mixed $default
     * @return mixed
     */
    public function getFromParavrac(string $key, $default)
    {
        return $this->paravrac->getGeneric($key) ?? $default;
    }
}
