<?php

namespace Entity\Model;

use Helpers\Filter\FilterTable;
use Helpers\QueryBuilder\QueryBuilder;
use Helpers\Trad\Trad;

class TypeDoc extends QueryBuilder
{
    protected string $table = 'TYPDOC with (nolock)';
    protected string $primaryKey = 'TYPE';
    protected string $libelle = 'LIBELLE';
    public string $titre;

    public function getArchitecture(\Helpers\Trad\ITrad $trad)
    {
        return  [
            'TYPE' =>
            [
                'nomChamp' => $trad->t('Type'),
                'type' => 'ZOOM',
                'taille' => 7,
                'obligatoire' => true
            ],
            'LIBELLE' =>
            [
                'nomChamp' => $trad->t('Libelle'),
                'type' => 'TEXTE',
                'taille' => 30,
                'obligatoire' => true

            ],
            'HAPLUS' =>
            [
                'nomChamp' =>$trad->t("Acheteur autorisé"),
                'type' => 'RADIO',
                'taille' => 4,
                'obligatoire' => true

            ],
            'NIV_CONF' =>
            [
                'nomChamp' => $trad->t('Niveau Configuration'),
                'type' => 'TEXTE',
                'taille' => 30,
                'genre' => 'int',
                'obligatoire' => true

            ],
            'INTERFA' =>
            [
                'nomChamp' => $trad->t('Interface'),
                'type' => 'RADIO',
                'taille' => 4,
                'obligatoire' => true

            ],
            'TXLIVEXCD' =>
            [
                'nomChamp' => $trad->t('Taux Live xcd'),
                'type' => 'TEXTE',
                'taille' => 30,
                'genre' => 'float',
                'obligatoire' => true

            ],
            'MTLIVEXCD' =>
            [
                'nomChamp' => $trad->t('Montant Live xcd'),
                'type' => 'TEXTE',
                'taille' => 30,
                'genre' => 'int',
                'obligatoire' => true

            ],
            'DATE_DEB' =>
            [
                'nomChamp' => $trad->t('Date de Fin'),
                'type' => 'DATE',
                'taille' => 8,
                'obligatoire' => true

            ],
            'DATE_FIN' =>
            [
                'nomChamp' =>$trad->t("Date de Début"),
                'type' => 'DATE',
                'taille' => 8,
                'obligatoire' => true

            ],
            'NVIPBOXAP' =>
            [
                'nomChamp' => $trad->t('Niveau BoxAp'),
                'type' => 'RADIO',
                'taille' => 4,
                'obligatoire' => true

            ],
            'TYDO_IND_IMPR_PAR_DEM' =>
            [
                'nomChamp' => $trad->t('Libelle'),
                'type' => 'RADIO',
                'taille' => 4,
                'obligatoire' => true

            ],


        ];
    }


    /**
     * @param array<string> $listSelectedField
     * @param array<mixed> $param
     */
    protected function fetchAllBaseQuery(
        array $listSelectedField,
        array &$param
    ) :string {
        $hasTrad = false;
        $listSelectedField = array_map(function ($field) use (&$hasTrad) {
            if (in_array($field, ['LIBELLE', 'TYPDOC.LIBELLE'])) {
                $hasTrad = true;
                // TODO : factorize with AbsRepository::selectTrad & AbsRepository::joinTrad
                return '(CASE 
                        WHEN Trad.TRAD IS NOT NULL
                        THEN Trad.TRAD
                        ELSE TYPDOC.LIBELLE
                    END) AS LIBELLE';
            }
            return $field;
        }, $listSelectedField);

        if (empty($listSelectedField)) {
            $listSelectedField = [
                'TYPE',
                '(CASE 
                    WHEN Trad.TRAD IS NOT NULL
                    THEN Trad.TRAD
                    ELSE TYPDOC.LIBELLE
                END) AS LIBELLE',
                'HAPLUS',
                'NIV_CONF',
                'INTERFA',
                'TXLIVEXCD',
                'MTLIVEXCD',
                'DATE_DEB',
                'DATE_FIN',
                'NVIBOXAP',
                'TYDO_IND_IMPR_PAR_DEM',
                'DEPASS_DELAI_LIVRAISON',
            ];

            $hasTrad = true;
        }

        if ($hasTrad) {
            $param[':_lang'] = $_SESSION['Session_Langue'];
        }
        return parent::fetchAllBaseQuery($listSelectedField, $param)
            . ($hasTrad ?
                'LEFT JOIN TXTTRAD Trad WITH(NOLOCK) 
                    ON Trad.COD_PROD = \'OXAPROC\' 
                    AND Trad.TXT = TYPDOC.LIBELLE COLLATE French_BIN 
                    AND Trad.COD_LNG = :_lang' : '');
    }
}
