<?php

namespace Modules\Stock\Trait\Controller;

use Entity\Model\Sarticle;
use Entity\Model\SbeEnte;
use Entity\Model\SbentLo;
use Entity\Model\Sborlot;
use Entity\Model\SbsEnte;
use Entity\Model\SbtEnte;
use Entity\Model\SbtLots;
use Entity\Model\SdsEnte;
use Entity\Model\SdsLign;
use Entity\Model\SdsPlan;
use Entity\Model\Sstocklo;
use Exception;
use Helpers\ChampsSupplementaire\ChampSupp;
use Modules\Stock\Enum\TypeEnteteEnum;
use Modules\Stock\Service\ListManagerService;

trait LigneControllerTrait
{
    /**
     * @param int|string $no
     * @param int $noLigne
     * @param array<string,mixed> $ligne
     * @param string $codeTab
     * @param TypeEnteteEnum|null $typeEntete
     * @return string
     * @throws Exception
     */
    public function getLigneResponse(int|string $no, int $noLigne, array $ligne, string $codeTab, ?TypeEnteteEnum $typeEntete = null): string
    {
        $idCs = ChampSupp::autoCompletCodeEnr((string) $no, $noLigne);
        $lignesPlanning = (new SdsPlan($this->pdo))->findAll(['NO_DS' => $ligne['NO_DS'], 'NO_LIG_DS' => $ligne['NO_LIG_DS']]);
        $ligneDemandeService = (new SdsLign($this->pdo))->find(['NO_DS' => $ligne['NO_DS'], 'NO_LIG_DS' => $ligne['NO_LIG_DS']]);

        $lots = [];
        if ($typeEntete) {
            $lots = match ($typeEntete) {
                TypeEnteteEnum::BS => (new Sborlot($this->pdo))->findAll(['NO_BS' => $no, 'NO_LIG' => $noLigne]),
                TypeEnteteEnum::BT => (new SbtLots($this->pdo))->findAll(['NO_BT' => $no, 'NO_LIG_BT' => $noLigne]),
                TypeEnteteEnum::BE => (new SbentLo($this->pdo))->findAll(['NO_BE' => $no, 'NO_LIG' => $noLigne]),
                default => []
            };
        }

        $enteteEntity = match ($typeEntete) {
            TypeEnteteEnum::BE => (new SbeEnte($this->pdo))->find($no),
            TypeEnteteEnum::BS => (new SbsEnte($this->pdo))->find($no),
            TypeEnteteEnum::BT => (new SbtEnte($this->pdo))->find($no),
            TypeEnteteEnum::DS => (new SdsEnte($this->pdo))->find($no),
            default => null
        };

        $sstocklo = new Sstocklo($this->pdo);
        $stockLocal = match ($typeEntete) {
            TypeEnteteEnum::DS, TypeEnteteEnum::BS => $sstocklo->find(['SITE' => $enteteEntity['SITE'], 'CODE_ART' => $ligne['ART']]),
            TypeEnteteEnum::BT, TypeEnteteEnum::BE => $sstocklo->find(['SITE' => $enteteEntity['SITE'], 'CODE_ART' => $ligne['CODE_ART']]),
            default => null
        };

        $stockArticle = (new Sarticle($this->pdo))->find(['CODE_ART' => $stockLocal['CODE_ART']], ['UNITE_STK']);

        return $this->renderer->render('@Stock/Composant/_ligne', [
            'idCs' => $idCs,
            'lots' => $lots,
            'codeTab' => $codeTab,
            'stockLocal' => $stockLocal,
            'typeEntete' => $typeEntete,
            'lignesPlanning' => $lignesPlanning,
            'uniteStockee' => $stockArticle['UNITE_STK'],
            'ligneDemandeService' => $ligneDemandeService,
            'listeTypeReappro' => (new ListManagerService($this->getTranslationService()))->getChoixTypeReapproList(),
        ]);
    }
}
