<?php

namespace Modules\RechercheArticles\Service;

use Entity\Model\OxaUserPanier;
use Entity\Model\OxaUserPanierCsDon;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Modules\Ajax\Repository\Listes;

class OutilArticleService
{
    private \PDO $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }


    /**     *
     * @param string|null $centreLivraison
     * @param string|null $user
     * @param string|null $typeDoc
     * @param string|null $codeSI
     * @param string|null $codeCDC
     * @param string|null $codeSCA
     * @param string|null $csArtFourn
     * @param string|null $codeSfam
     * @param string|null $codeArt
     * @return array<mixed>
     */
    public function generateInitCS(
        ?string $centreLivraison = null,
        ?string $user = null,
        ?string $typeDoc = null,
        ?string $codeSI = null,
        ?string $codeCDC = null,
        ?string $codeSCA = null,
        ?string $csArtFourn = null,
        ?string $codeSfam = null,
        ?string $codeArt = null,
        string $codTab = 'DA_LIGNE'
    ) {
        $arrayInitCs = [];

        $listeCSForInit[] = ['TAB_SRC' => 'CENTRE',     'ID' => $centreLivraison];
        $listeCSForInit[] = ['TAB_SRC' => 'HASTUTIL',   'ID' => $user];
        $listeCSForInit[] = ['TAB_SRC' => 'TYPDOC',     'ID' => $typeDoc];
        $listeCSForInit[] = ['TAB_SRC' => 'SECTIMPU',   'ID' => ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'SECTIMPU', $codeSI)];
        $listeCSForInit[] = ['TAB_SRC' => 'CODECOMP',   'ID' => ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'CODECOMP', $codeCDC)];
        $listeCSForInit[] = ['TAB_SRC' => 'SSCPTANA',   'ID' => ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'SSCPTANA', $codeSCA)];
        $listeCSForInit[] = ['TAB_SRC' => 'ARTFOURN',   'ID' => $csArtFourn];
        $listeCSForInit[] = ['TAB_SRC' => 'SSFAMILL',   'ID' => $codeSfam];
        $listeCSForInit[] = ['TAB_SRC' => 'ARTICLES',   'ID' => $codeArt];

        foreach ($listeCSForInit as $val) {
            foreach (ChampSupp::initCs($codTab, $val['TAB_SRC'], $val['ID'], $this->pdo) as $initCS) {
                $arrayInitCs['CS_EXTENTION'.Constante::spliter.$codTab.Constante::spliter.''.Constante::spliter.$initCS['CHP_DESTI']] = $initCS['VALEUR'];
            }
        }

        foreach (ChampSupp::initCsPanier($this->pdo) as $initCS) {
            $arrayInitCs['CS_EXTENTION'.Constante::spliter.$codTab.Constante::spliter.''.Constante::spliter.$initCS['CHP_DESTI']] = $initCS['VALEUR'];
        }

        return $arrayInitCs;
    }


    /**
     * @param array<mixed> $infoLigne
     */
    public function gestionCsPanier(
        string $idPanier,
        string $idUser,
        string $codTab,
        $infoLigne,
        string $idLignePanier = '',
        bool $duplicate = false,
        bool $modeImport = false,
        string $codeSte = ''
    ) : void {
        $champSupp = new ChampSupp($this->pdo);
        $listeCS = $champSupp->getInfoCS($codTab);
        $oxaUserPanierCsDon = new OxaUserPanierCsDon($this->pdo);



        $infoPanier = (new OxaUserPanier($this->pdo))->find([
            'id_user' => $idUser,
            'id_panier' => $idPanier,
        ]);

        foreach ($listeCS as $cs) {
            $valueCS = '';
            $csExist = $oxaUserPanierCsDon->find([
                'id_user' => $idUser,
                'id_panier' => $idPanier,
                'NUM_FIX' => $cs['NUM_FIX'],
                'COD_TAB' => $codTab,
                'ID_LIGNE' => $idLignePanier,
            ]);

            if (($duplicate && Sql::boolSql($cs['NONDUPLI'])) || $csExist) {
                continue;
            }

            if ($cs['TAB_SOUR']) {
                $codEnr = '';
                switch ($cs['TAB_SOUR']) {
                    case 'ARTFOURN':
                        $codEnr = str_pad($infoLigne['COD_ART'], 13, ' ', STR_PAD_RIGHT).str_pad($infoLigne['COD_FOURN'], 12, ' ', STR_PAD_RIGHT);
                        break;
                    case 'ARTICLES':
                        $codEnr = $infoLigne['COD_ART'];
                        break;
                    case 'DA_ENTET':
                        $valueCS = ChampSupp::getValueCSPanier($cs['TAB_SOUR'], $idUser, $idPanier, $cs['CHP_SOUR'], $this->pdo);
                        break;
                    case 'SECTIMPU':
                        $codEnr = ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'SECTIMPU', $infoLigne['SI_CODE'], $infoPanier['CODE_STE']);
                        break;
                    case 'CODECOMP':
                        $codEnr = ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'CODECOMP', $infoLigne['CDC_CODE'], $infoPanier['CODE_STE']);
                        break;
                    case 'SSCPTANA':
                        $codEnr = ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'SSCPTANA', $infoLigne['SCA_CODE'], $infoPanier['CODE_STE']);
                        break;
                    case 'SSFAMILL':
                        $codEnr = $infoLigne['COD_SFAM'];
                        break;
                    //entete
                    case 'CENTRE':
                        $codEnr = $infoPanier['LIV_CTR'];
                        break;
                    case 'HASTUTIL':
                        $codEnr =  $_SESSION['Session_Login'];
                        break;
                    case 'TYPDOC':
                        $codEnr = sql::paravrac($this->pdo, 'OXAPROC', 'TYPDOCUMENT');
                        break;
                }

                if ($cs['TAB_SOUR'] != 'DA_ENTET') {
                    $valueCS = ChampSupp::getValueCS($cs['TAB_SOUR'], $codEnr, $cs['CHP_SOUR'], $this->pdo);
                }
            }

            $oxaUserPanierCsDon->insert([
                'id_user' => $idUser,
                'id_panier' =>  $idPanier,
                'NUM_FIX' => $cs['NUM_FIX'],
                'VALEUR'=> $valueCS,
                'ID_LIGNE' => $idLignePanier,
                'COD_TAB' => $codTab,
            ]);
        }

        $specifique = new \Helpers\Additional\Specifique\Specifique();
        $namespaceSpecifique = $specifique->specifiqueFunction('\\Modules\\Panier\\Service\\SpecPanierLigneService');
        if (class_exists($namespaceSpecifique)) {
            \Helpers\Additional\Specifique\Specifique::specifique('\\Modules\\Panier\\Service\\SpecPanierLigneService', 'initAxesImputation', $this->pdo, $_SESSION['id_user'], $_SESSION['id_panier'], $infoLigne);
        }
    }

    public function controleACD(?string $idAcd = null, ?string $codeFrs = null, ?string $centreEmetteur = null, ?string $centreLivraison = null):string
    {
        if ($idAcd) {
            $paramAcd = [
                'valide'                => '1',
                'CODE_FOUR'             => $codeFrs,
                'CENTRE_EMETTEUR'       => $centreEmetteur,
                'CENTRE_LIVRAISON'      => $centreLivraison,
                'filter_egal_CODE_ACD'  => $idAcd,
            ];
            $liste = new Listes($this->pdo);
            $acdFound = $liste->AccordCadre($paramAcd);

            if ($acdFound) {
                return $idAcd;
            }
        }

        return '';
    }
}
