<?php

namespace Entity\Model;

use Helpers\Outil\ArrayHelper;
use Helpers\QueryBuilder\QueryBuilder;

class ParFiche extends QueryBuilder
{
    protected string $table = 'PARFICHE with (nolock)';
    protected string $primaryKey = '';
    protected array $primKey = ['CODE_TAB', 'CODE_CHP'];

    /**
     * @var array<array<mixed>>
     */
    private static ?array $cache = null;

    /**
     * @param array<string> $selectTables
     * @param array<mixed> $post
     * @param mixed $option
     * @return array<array<mixed>>
     */
    public function fetchAll(array $selectTables = [], array $post = [], $option = null)
    {
        if (self::$cache === null) {
            // TODO : use wincache too ?
            self::$cache = parent::fetchAll([], ['tri_NUM_CHP' => 'ASC']);
        }
        return self::$cache;
    }


    /**
     * @return array<string>[]
     */
    public function getForTable(string $code): array
    {
        return ArrayHelper::indexBy($this->fetchAll(), 'CODE_TAB')[$code] ?? [];
    }

    /**
     * @return array<string>[]
     */
    public function getContactFour(): array
    {
        return $this->getForTable('CTCFOU');
    }
}
