<?php

namespace Modules\Additional\Specifique\Client56\Modules\Panier\Service;

use Entity\Model\Codgest;
use Entity\Model\Dilispec;
use Entity\Model\OxaUserPanier;
use Entity\Model\OxaUserPanierLigne;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Sql;
use Modules\GlobalApplication\Outil\InitAxesImpDa;
use Modules\GlobalApplication\Outil\InitAxesImputations;
use Modules\Panier\Service\PanierLigneService;
use Modules\RechercheArticles\Service\OutilArticleService;

class SpecPanierLigneService
{
    public static function callStdCS(\PDO $pdo, \PDO $pdoOxaft, string $idLigne) : void
    {
        $panierEntete = (new OxaUserPanier($pdo))->find([
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $_SESSION['id_panier'],
        ]);

        $panierLigne = (new OxaUserPanierLigne($pdo))->find($idLigne);

        (new OutilArticleService($pdo))->gestionCsPanier(
            $_SESSION['id_panier'],
            $_SESSION['id_user'],
            'DA_LIGNE',
            $panierLigne,
            $idLigne,
            false,
            false,
            $panierEntete['CODE_STE']??'',
        );
    }

    /**
     * @param array<mixed> $info_ligne
     */
    public static function initAxesImputation(\PDO $pdo, string $id_panier, string $id_ligne, $info_ligne) : void
    {
        //specifique panier/clas_panier::877 Pour le client N°56

        $oxaUserPanier = new OxaUserPanier($pdo);
        $infoEntete = $oxaUserPanier->find([
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $id_panier,
        ]);

        $cgParId = \Helpers\Outil\Roles::droit($pdo, 'CODE_GESTION_PAR_ID')? 1:0;
        $cgEntity = new Codgest($pdo);

        // TODO : verify find param
        if ($cgParId) {
            $infoCg = $cgEntity->find(['CODE_GES' => $infoEntete['CG_CODE']]);
        } else {
            $infoCg = $cgEntity->find(['CODE_AFF' => $infoEntete['CG_CODE']]);
        }

        $initImputation = new InitAxesImputations($pdo, 'DA', '', [
            'CODE_STE'      => $infoEntete['CODE_STE'],
            'TYP_DOC'       => '',
            'SECTIMP'       => $infoEntete['SI_CODE'],
            'CODECOMP'      => $infoEntete['CDC_CODE'],
            'SSCPTEANA'     => $infoEntete['SCA_CODE'],
            'ID_CG'         => $infoCg['ID_CG']??'',
            'CODGES'        => $infoCg['CODGES']??'',
            'LIBGES'        => $infoCg['LIBGES']??'',
        ]);
        $initImput = $initImputation->getInitImputation($info_ligne['COD_ART'], $info_ligne['COD_SFAM']);

        //INIT SI DEPUIS LE CDC
        $numFixe = Sql::dilispec($pdo, 'PARAMCC', 'NCS_CODECOMP_LIEUANA_VAL');
        $valueCS_SI = ChampSupp::getValueCS('CODECOMP', $info_ligne['CDC_CODE'], $numFixe, $pdo);

        if (!$valueCS_SI) {
            $valueCS_SI = $initImput['SI'];
        }

        //INIT LE SCA DEPUIS LE CDC
        $numFixe = Sql::dilispec($pdo, 'PARAMCC', 'NCS_CODECOMP_PROJET_VAL');
        $valueCS_SCA = ChampSupp::getValueCS('CODECOMP', $info_ligne['CDC_CODE'], $numFixe, $pdo);

        if (!$valueCS_SCA) {
            $valueCS_SCA = $initImput['SCA'];
        }

        $oxaUserPanierLigne = new OxaUserPanierLigne($pdo);
        $oxaUserPanierLigne->update([
            'SI_CODE' => $valueCS_SI,
            'SCA_CODE' => $valueCS_SCA
        ], [
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $id_panier,
            'id_lig' => $id_ligne
        ]);
    }
}
