<?php

namespace Modules\Commande\Controller;

use Modules\GlobalApplication\Repository\LicenceOxalys;
use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Ariane\ArianeSub;
use Helpers\Trad\Trad;
use Helpers\Trad\ITrad;
use Helpers\Session\Enums\MenuTopEnum;
use Helpers\Session\Session;
use Entity\Model\TpEnt;
use Entity\Model\TpEvt;
use Entity\Model\CaEntet;
use Entity\Model\TyEvtStp;
use Modules\Commande\Repository\Commande;

class TermeDePaiementCommandeController extends Controller
{
    private Commande $repository;

    /**
     * trad
     *
     * @var ITrad
     */
    private $trad;

    public function init(): void
    {
        $this->repository = new Commande(
            $this->pdo,
        );

        $this->trad = $this->getTranslationService();
    }

    public function indexAction(
        TpEnt $tpEnt,
        TpEvt $tpEvt,
        TyEvtStp $tyEvtStp,
        CaEntet $caEntet,
    ): string {
        $post       = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');
        $trad       = $this->getTranslationService();
        $MODULE     = $this->request->getAttribute('MODULE') ?? 'Commande';

        $commande = $this->repository->commandeEntete($idCommande);
        $devise   = $caEntet->find(['NO_CDE' => $idCommande], ['DEVISE'])['DEVISE'] ?? '';

        $titre = Trad::strBuild(
            $trad->t('Liste des termes de paiement de la commande : @1@'),
            $idCommande.($commande['AVENANT']? ' - '.$commande['AVENANT']:'')
        );

        $tps = $tpEnt->findAll(['NUM_CDE' => $idCommande, 'TYPEOBJ' => 'CA_ENTET']);
        foreach ($tps as &$tp) {
            $evts         = $tpEvt->findAll([
                'NUM_TP' => $tp['NUM_TP'],
                'NUM_CDE' => $tp['NUM_CDE']
            ]);
            if (count($evts) > 0) {
                $mapTypeEvent = array_column(
                    $tyEvtStp->findAll([
                        'CODE_EVT' => array_column($evts, 'EVENEMENT'),
                    ]),
                    null,
                    'CODE_EVT'
                );
                $declencheurs = [];
                foreach ($evts as $evt) {
                    $type = $mapTypeEvent[$evt['EVENEMENT']];
                    if (!empty($type['LIB_EVT'])) {
                        $declencheurs[] = $trad->t($type['LIB_EVT']); // TODO: shoud be done in SQL
                    }
                }
                $tp['DECLENCHEUR'] = implode(', ', $declencheurs); // TODO: move to view
            }
        }
        unset($tp);

        $visu = match (true) {
            str_contains(strtolower(Ariane::getBackPath()), 'suivi'), str_contains(strtolower(Ariane::getBackPath()), 'panier') => 'P2P',
            default => 'S2C'
        };

        match (true) {
            str_contains(strtolower(Ariane::getBackPath()), 'suivi'), str_contains(strtolower(Ariane::getBackPath()), 'panier') => Ariane::generate(2, $trad->t('Termes de paiement'), $post),
            default => ArianeSub::generate(0, 6, $trad->t('Termes de paiement'), $this->router->generateUri('Commande.TermeDePaiementCommande.index', ['idCommande' => $idCommande, 'MODULE' => 'Commande']), 1)
        };

        $menuLeft = match ($visu) {
            "S2C" => 'Commande.GestionCommande.index',
            // P2P
            default => 'Suivi.SuiviCde.liste'
        };

        $menuTop = match ($visu) {
            "S2C" => 'CollecteBesoins',
            // P2P
            default => 'eproc'
        };

        return $this->renderer->render('@Commande/TermPaie/index', [
            'menuActive' => $menuTop,
            'menuLeft' => $menuLeft,
            'idCommande' => $idCommande,
            'cde' => array_merge($commande, $post),
            'h1' => $this->trad->t('Demandes d\'achat'),
            'NO_CA' => 'CA_ENTET',
            'post' => $post,
            'tps' => $tps,
            'devise' => $devise,
            'titre' => $titre,
            'MODULE' => $MODULE,
            'commande' => $commande,
            'visu' => $visu
        ]);
    }
}
