<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;
use Helpers\Trad\Trad;

class TypeTVA extends QueryBuilder
{
    protected \PDO $pdo;

    protected string $table = 'TYPETVA with (nolock)';

    protected string $primaryKey = 'CODE_TTV';
    protected string $libelle = 'LIBELLE';
    public string $titre;
    public function __construct(\PDO $pdo)
    {
        
        $this->pdo = $pdo;
    }

    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
     */
    public function findAll($id, array $selectTables = [], $post = [], $entity = null, $exact = false)
    {
        if (is_string($id)) {
            $arrayTmp = $id;
        } else {
            asort($id);
            $arrayTmp = implode('_', $id);
        }

        $winCacheResult = \Helpers\Cache\WinCache::surchargeGetUcache('PARAM' . $_SESSION['_brokernumber']);

        if (isset($winCacheResult[$arrayTmp])) {
            return $winCacheResult[$arrayTmp];
        } else {
            $returnReqArray = parent::findAll($id, $selectTables, $post, $entity, $exact);
            $winCacheResult[$arrayTmp] = $returnReqArray;
            \Helpers\Cache\WinCache::surchargeSetUcache('PARAM' . $_SESSION['_brokernumber'], $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' : '');
    }
}
