<?php

namespace Entity\Model;

use Exception;
use Helpers\QueryBuilder\QueryBuilder;
use Helpers\Session\ParseBase1;

class TypeTVA extends QueryBuilder
{
    protected string $table = 'TYPETVA with (nolock)';
    protected string $primaryKey = 'CODE_TTV';
    protected string $libelle = 'LIBELLE';
    public string $titre;

    public function getArchitecture(\Helpers\Trad\ITrad $trad)
    {
        return
            [
                'CODE_TTV' => [
                    'nomChamp' => $trad->t('Code Type Tva'),
                    'type' => 'TEXTE',
                    'taille' => 1,
                    'obligatoire' => true
                ],
                'LIBELLE' => [
                    'nomChamp' => $trad->t('Libelle'),
                    'type' => 'TEXTE',
                    'taille' => 20,
                    'obligatoire' => true
                ]
            ];
    }

    /**
     * findAll
     *
     * @param mixed $id
     * @param array<mixed> $selectTables
     * @param array<mixed> $post
     * @param mixed $entity
     * @param mixed $exact
     * @return mixed
     * @throws Exception
     */
    public function findAll($id, array $selectTables = [], $post = [], $entity = null, $exact = false)
    {
        $arrayTmp = null;
        if (is_string($id)) {
            $arrayTmp = $id;
        } elseif (is_array($id)) {
            asort($id);
            $arrayTmp = implode('_', $id);
        }

        $winCacheResult = \Helpers\Cache\Cache::surchargeGetUcache('PARAM' . (new ParseBase1())->getBroker());

        if (isset($winCacheResult[$arrayTmp])) {
            return $winCacheResult[$arrayTmp];
        } else {
            $returnReqArray = parent::findAll($id, $selectTables, $post, $entity, $exact);
            $winCacheResult[$arrayTmp] = $returnReqArray;
            \Helpers\Cache\Cache::surchargeSetUcache('PARAM' . (new ParseBase1())->getBroker(), $winCacheResult);
            return $returnReqArray;
        }
    }


    /**
     * @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', 'TYPETVA.LIBELLE'])) {
                $hasTrad = true;
                // TODO : factorize with AbsRepository::selectTrad & AbsRepository::joinTrad
                return '(CASE 
                        WHEN t.TRAD IS NOT NULL
                        THEN t.TRAD
                        ELSE TYPETVA.LIBELLE
                    END) AS LIBELLE';
            }
            return $field;
        }, $listSelectedField);

        if (empty($listSelectedField)) {
            $listSelectedField = [
                'CODE_TTV',
                '(CASE 
                    WHEN t.TRAD IS NOT NULL
                    THEN t.TRAD
                    ELSE TYPETVA.LIBELLE
                END) AS LIBELLE',
                'CODE_TTV',
            ];

            $hasTrad = true;
        }

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