<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;
use Helpers\Trad\Trad;
use Helpers\Session\ParseBase1;

class Unites extends QueryBuilder
{
    protected string $table = 'UNITES with (nolock)';
    protected string $primaryKey = 'CODE_UNI';
    protected string $libelle = 'LIBELLE';
    public string $titre;

    public function getArchitecture(\Helpers\Trad\ITrad $trad)
    {
        return  [
            'CODE_UNI' => [
                'nomChamp' =>$trad->t("Code Unité"),
                'type' => 'TEXTE',
                'taille' => 7,
                'obligatoire' => true
            ],
            'LIBELLE' => [
                'nomChamp' => $trad->t('Libelle'),
                'type' => 'TEXTE',
                'taille' => 20,
                'obligatoire' => true
            ],
            'NB_DECIM' => [
                'nomChamp' =>$trad->t("Nombre Décimale"),
                'type' => 'TEXTE',
                'taille' => 2,
                'obligatoire' => true
            ]
          
        ];
    }

    /**
     * @return array<mixed>
     */
    public function getMap() :array
    {
        return array_column(
            array_map(
                function (array $row) {
                    return ['NB_DECIM' => intval($row['NB_DECIM'])] + $row;
                },
                (new Unites($this->pdo))->fetchAll()
            ), // TODO : use cache
            null,
            'CODE_UNI'
        );
    }

    /**
     * find
     *
     * @param  mixed $id
     * @param  array<mixed> $selectTables
     * @param  array<mixed> $post
     * @param  mixed $option
     * @return mixed
     */
    public function find($id, array $selectTables = [], $post = [], $option = false)
    {
        if (is_string($id)) {
            $arrayTmp = $id;
        } else {
            asort($id);
            $arrayTmp = implode('_', $id);
        }

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