<?php

namespace Modules\Panier\Service;

use Entity\Model\Bdd;
use Helpers\Outil\ArrayHelper;
use Helpers\Outil\Outils;
use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Helpers\Trad\ITrad;
use Modules\Panier\Repository\PanierEntete;

class PanierBuildService
{
    /**
     * @var array<mixed>
     */
    private array $listeFrs = [];
    /**
     * @var array<mixed>
     */
    private array $listeGArtParCritere = [];
    /**
     * @var array<mixed>
     */
    private array $totalPanier = [];
    /**
     * @var array<mixed>
     */
    private array $totalFraisAnnexes = [];
    /**
     * @var array<mixed>
     */
    private array $listeTypeAchatByFrs = [];
    /**
     * @var array<mixed>
     */
    private array $typeAchatParDefautByFrs = [];
    /**
     * @var array<mixed>
     */
    private array $listeBDDByFrs = [];

    /**
     * @param array<mixed> $parametrage
     */
    public function __construct(
        private \PDO $pdo,
        private \PDO $pdoOxaFT,
        private array $parametrage,
        private ITrad $trad,
        private string $centre,
    ) {
    }

    /**
     * @param array<mixed> $idPanier
     */
    public function buildPanier(array $idPanier):void
    {
        $panierEntete = new PanierEntete($this->pdo);
        $this->listeFrs = $panierEntete->getListeFrsPanier($idPanier);
        $panierFrsService = new PanierFrsService($this->pdo);

        $listeFrsWhereIn = [];
        foreach ($this->listeFrs as &$frs) {
            $frs['ID_BLOC'] = str_replace('|', Constante::oxaPipe, $frs['CRITERE_REGROUP']);

            $listeFrsWhereIn[$frs['COD_FOURN']] = $frs['COD_FOURN'];

            $frs['COD_DEV'] = explode('|', $frs['CRITERE_REGROUP'])[1];
            
            $listeAdresse = $panierFrsService->adresseCommande($this->centre, $frs['COD_FOURN']);
            $frs['listeAdresse'][''] = $this->trad->t('Choix du site de commande');
            foreach ($listeAdresse as $adr) {
                // comparer si on match les valeurs de l'adresse avec les valeurs de l'entête fourniseur
                if ($adr['CODPAYS'] == $frs['CODE_PAYS']
                    && $adr['CPOSTAL'] == $frs['CPOSTAL']
                    && $adr['VILLE1'] == $frs['VILLE1']
                    && $adr['ADRESSE'] == $frs['ADR']
                ) {
                    $frs['CODE_ADR'] = $adr['COD_ADR'];
                }
                $frs['listeAdresse'][$adr['COD_ADR']] = $adr['VILLE'].' - '.$adr['LIBELLE'];
            }
        }

        if ($listeFrsWhereIn) {
            $typeAchatParDefaut = Sql::paravrac($this->pdo, 'PARAMFOU', 'INITTAC');
            $listeTypeAchatTmp = $panierEntete->getListeTypeAchatByFrs($listeFrsWhereIn, $typeAchatParDefaut);

            $libelle = Outils::convertLibelleGlobal($this->pdo, 'TYPEACHAT', '', $typeAchatParDefaut);
            $this->listeTypeAchatByFrs[Constante::fournisseurStock][$typeAchatParDefaut] = $libelle;
            $this->typeAchatParDefautByFrs[Constante::fournisseurStock] = $typeAchatParDefaut;

            foreach ($listeTypeAchatTmp as $typeAch) {
                $this->listeTypeAchatByFrs[$typeAch['CODE']][$typeAch['TACFOTAC']] = $typeAch['LIB_TAC'];

                if (sql::boolSql($typeAch['TACFODEF'])) {
                    $this->typeAchatParDefautByFrs[$typeAch['CODE']] = $typeAch['TACFOTAC'];
                }
            }

            $listeBDDTmp = (new Bdd($this->pdo))->getAllBDD('Fournisseur', $listeFrsWhereIn, 'D');
            $this->listeBDDByFrs = ArrayHelper::indexBy($listeBDDTmp, 'CODE_FOU');
        }

        $this->listeGArtParCritere = (new PanierService($this->pdo))->getListeArticle($this->pdoOxaFT, $this->parametrage);

        foreach ($this->listeGArtParCritere as $listeArticle) {
            foreach ($listeArticle as $ligne) {
                if ($ligne['type_de_ligne'] == '2') {
                    if (isset($this->totalFraisAnnexes[$ligne['CRITERE_REGROUP']])) {
                        $this->totalFraisAnnexes[$ligne['CRITERE_REGROUP']] += $ligne['qte'] * $ligne['PX_ART'];
                    } else {
                        $this->totalFraisAnnexes[$ligne['CRITERE_REGROUP']] = $ligne['qte'] * $ligne['PX_ART'];
                    }
                }
        
                if (isset($this->totalPanier[$ligne['CRITERE_REGROUP']])) {
                    $this->totalPanier[$ligne['CRITERE_REGROUP']] += $ligne['qte'] * $ligne['PX_ART'];
                } else {
                    $this->totalPanier[$ligne['CRITERE_REGROUP']] = $ligne['qte'] * $ligne['PX_ART'];
                }
            }
        }
    }

    /**
     * @return array<mixed>
     */
    public function getListeFrs()
    {
        return $this->listeFrs;
    }

    /**
     * @return \stdClass{byFrs:array<mixed>,totalFraisAnnexes:array<mixed>}
     */
    public function getTotalPanier():object
    {
        return (object) [
            'totalPanier'       => $this->totalPanier,
            'totalFraisAnnexes' => $this->totalFraisAnnexes,
        ];
    }

    /**
     * @return \stdClass{byFrs:array<mixed>,DefautByFrs:array<mixed>}
     */
    public function getTypeAchat():object
    {
        return (object) [
            'byFrs'             => $this->listeTypeAchatByFrs,
            'DefautByFrs'       => $this->typeAchatParDefautByFrs,
        ];
    }

    /**
     * @return array<mixed>
     */
    public function getListeArticle()
    {
        return $this->listeGArtParCritere;
    }

    /**
     * @return array<mixed>
     */
    public function getBDD()
    {
        return $this->listeBDDByFrs;
    }
}
