<?php
namespace TwigExtension;

use PDO;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Entity\Model\Pays;
use Helpers\Trad\ITrad;
use Modules\Ajax\Repository\Listes;
use Modules\Commande\Repository\ModificationDetailCommande;
use Psr\Container\ContainerInterface;
use Skeleton\Router;

class ListeValueExtension extends AbstractExtension
{

    /** @phpstan-ignore-next-line */
    private readonly ITrad $trad;
    
    public function __construct(Router $router, private readonly PDO $pdo, ContainerInterface $container)
    {
        unset($router);
        $this->trad = $container->get(ITrad::class);
    }

    /**
     * @return array<mixed>
     */
    public function getFunctions()
    {
        return [
            new TwigFunction('listePays', $this->listePays(...), [
                'is_safe' => [
                    'html'
                ]
            ]),
            new TwigFunction('listePeriodicite', $this->listePeriodicite(...), [
                'is_safe' => [
                    'html'
                ]
            ]),
            new TwigFunction('listeTypeAchat', $this->listeTypeAchat(...), [
                'is_safe' => [
                    'html'
                ]
            ]),
            new TwigFunction('listeModeTransport', $this->listeModeTransport(...), [
                'is_safe' => [
                    'html'
                ]
            ]),
            new TwigFunction('listeJours', $this->listeJours(...), [
                'is_safe' => [
                    'html'
                ]
            ]),
        ];
    }

    /**
     * @return array<mixed>
     */
    public function listePays()
    {
        $listePaysTmp = (new Pays($this->pdo))->fetchAll([], ['tri_LIBELLE' => 'asc']);
        $listePaysTmp[''] = [
            'CODE_PAYS' => '',
            'LIBELLE' => '',
        ];
        return array_column($listePaysTmp, 'LIBELLE', 'CODE_PAYS');
    }

    /**
     * @return array<mixed>|string
     */
    public function listePeriodicite(?string $code = null)
    {
        $return = [
            'M' => $this->trad->t('Mensuelle'),
            'T' => $this->trad->t('Trimestrielle'),
            'S' => $this->trad->t('Semestrielle'),
            'A' => $this->trad->t('Annuelle'),
        ];

        if ($code) {
            return $return[$code];
        } else {
            return $return;
        }
    }

    /**
     * @return array<mixed>
     */
    public function listeTypeAchat()
    {
        $listeTypeAchatTmp = (new Listes($this->pdo))->typeAchat([]);
        return array_column($listeTypeAchatTmp, 'LIB_TAC', 'TYP_ACH');
    }

    /**
     * @return array<mixed>
     */
    public function listeModeTransport()
    {
        $listeModeTransport = (new ModificationDetailCommande($this->pdo))->getListeModeTransport();

        $tabModeTransport = [];
        foreach ($listeModeTransport as $modeTransport) {
            if ($modeTransport['DES_ART']) {
                $libelle = $modeTransport['DES_ART'];
            } else {
                $libelle = $modeTransport['MOD_TRS'];
            }
            
            $tabModeTransport[$modeTransport['MOD_TRS']] = $libelle;
        }

        return $tabModeTransport;
    }

    /**
     * @return array<mixed>
     */
    public function listeJours()
    {
        $liste = [];

        for ($i=1; $i<32; $i++) {
            $liste[$i] = $i;
        }

        return $liste;
    }
}
