<?php

namespace Modules\GlobalApplication\Outil;

use Helpers\Outil\Sql;
use Entity\Model\SiSteArt;
use Entity\Model\SiSteFam;
use Entity\Model\SiSteSFa;

use Entity\Model\CdcSteAr;
use Entity\Model\CdcSteFa;
use Entity\Model\CdcSteSF;

use Entity\Model\StaSteFa;
use Entity\Model\ScaSteAr;
use Entity\Model\ScaSteSF;

class InitAxesImputations
{
    /**
     * @var \PDO $pdo
     */
    private $pdo;
    /**
     * @var array <mixed>
     */
    private $entete;

    /**
     * @var array <mixed>
     */
    private $arrayPriorites;

    /**
     * @var array <mixed>
     */
    private $paramSectorisation;
    
    /**
     *  @param array<mixed> $entete
    */
    public function __construct(\PDO $pdo, string $type, string $idDoc = '', $entete = [])
    {
        $this->pdo = $pdo;

        $INIT_IMP = '';

        if ($type == 'DA') {
            if ($entete) {
                $this->entete = $entete;
            } else {
                $DaEntet = new \Entity\Model\DaEntet($pdo);
                $this->entete = $DaEntet->find($idDoc);
            }

            $INIT_IMP = Sql::paravrac($pdo, 'INIT_IMPD', ($this->entete['TYP_DOC']??''));
            
            if (!$INIT_IMP) {
                $INIT_IMP = Sql::paravrac($pdo, 'INIT_IMPDA', '');
            }
        } elseif ($type == 'CDE') {
            if ($entete) {
                $this->entete = $entete;
            } else {
                $CaEntet = new \Entity\Model\CaEntet($pdo);
                $this->entete = $CaEntet->find($idDoc);
            }

            $INIT_IMP = Sql::paravrac($pdo, 'INIT_IMPC', $this->entete['TYP_DOC']);
               
            if (!$INIT_IMP) {
                $INIT_IMP = Sql::paravrac($pdo, 'INIT_IMPCA', '');
            }
        } elseif ($type == 'CDE_COMPTOIR_MNT') {
            $this->entete = $entete;

            $INIT_IMP = 'ART;DEM;|ART;DEM;|ART;DEM;';
        }

        // <P1SI>;<P2SI>;<P3SI>|<P1SCA>;<P2SCA>;<P3SCA>|<P1CDC>;<P2CDC>;<P3CDC>
        $priorites = explode('|', $INIT_IMP);
        $this->arrayPriorites = [
            'SI'    => explode(';', $priorites[0]),
            'SCA'   => explode(';', $priorites[1]),
            'CDC'   => explode(';', $priorites[2]),
        ];

        $this->paramSectorisation = [
            'SI' => \Helpers\Outil\Roles::droit($pdo, 'SISECTOR', 'PARAMSTE'),
            'SCA' => \Helpers\Outil\Roles::droit($pdo, 'SCASECTO', 'PARAMSTE'),
            'CDC' => \Helpers\Outil\Roles::droit($pdo, 'CDCSECTO', 'PARAMSTE'),
        ];
    }
    
    
    /**
    * @param array<mixed> $ligneDA
    * @return array<mixed>
    */
    public function getInitImputation(string $codeArt = '', string $ssFamille = '', string $famille = '', $ligneDA = [])
    {
        /** @var SiSteFam|CdcSteFa|StaSteFa $entityFam */
        $entityFam = '';
        /** @var SiSteSFa|CdcSteSF|ScaSteSF $entitySsFam */
        $entitySsFam = '';
        /** @var SiSteArt|CdcSteAr|ScaSteAr $entityArt */
        $entityArt = '';

        $keyEntete = '';
        $keyLigne = '';
        $session = '';

        $imputations = [];
        foreach ($this->arrayPriorites as $axesImputation => $arrayPriorites) {
            switch ($axesImputation) {
                case 'SI':
                    $keyLigne = $keyEntete = 'SECTIMP';
                    $session = 'Session_SectionImputation';

                    $entityArt = new \Entity\Model\SiSteArt($this->pdo);
                    $entitySsFam = new \Entity\Model\SiSteSFa($this->pdo);
                    $entityFam = new \Entity\Model\SiSteFam($this->pdo);
                    break;
                case 'CDC':
                    $keyEntete = 'CODECOMP';
                    $keyLigne = 'CODE_COMP';
                    $session = 'Session_CDC';

                    $entityArt = new \Entity\Model\CdcSteAr($this->pdo);
                    $entitySsFam = new \Entity\Model\CdcSteSF($this->pdo);
                    $entityFam = new \Entity\Model\CdcSteFa($this->pdo);
                    break;
                case 'SCA':
                    $keyLigne = $keyEntete = 'SSCPTEANA';
                    $session = 'Session_SCA';

                    $entityArt = new \Entity\Model\ScaSteAr($this->pdo);
                    $entitySsFam = new \Entity\Model\ScaSteSF($this->pdo);
                    $entityFam = new \Entity\Model\StaSteFa($this->pdo);
                    break;
            }

            foreach ($arrayPriorites as $priorites) {
                switch ($priorites) {
                    case 'ENT':
                        $imputations[$axesImputation] = $this->entete[$keyEntete];

                        if ($axesImputation == 'SI') {
                            $imputations['ID_CG']   = $this->entete['ID_CG']??'';
                            $imputations['CODGES']  = $this->entete['CODGES']??'';
                            $imputations['LIBGES']  = $this->entete['LIBGES']??'';
                        }
                        break;
                    case 'ART':
                        if ($codeArt) {
                            if ($this->paramSectorisation[$axesImputation]) {
                                $codeSte = $this->entete['CODE_STE'];
                            } else {
                                $codeSte = '';
                            }

                            $initAxe[$keyLigne] = $entityArt->find(['CODE_ART' => $codeArt, 'CODE_STE' => $codeSte])[$axesImputation] ?? '';
                            
                            if ($initAxe[$keyLigne] == '' && ($ssFamille) != '') {
                                $initAxe[$keyLigne] = $entitySsFam->find(['SFAMILL' => $ssFamille, 'CODE_STE' => $codeSte])[$axesImputation] ?? '';
                                
                                if ($initAxe[$keyLigne] == '') {
                                    $initAxe[$keyLigne] = $entityFam->find(['FAMILLE' => $famille, 'CODE_STE' => $codeSte])[$axesImputation] ?? '';
                                }
                            }

                            $imputations[$axesImputation] = $initAxe[$keyLigne]??'';
                        }
                        break;
                    case 'DEM'://specifique DA
                        $imputations[$axesImputation] = $_SESSION[$session];
                        break;
                    case 'DA'://specifique CDE
                        if (($ligneDA[$keyLigne]??'') != '') {
                            $imputations[$axesImputation] = $ligneDA[$keyLigne];

                            if ($axesImputation == 'SI') {
                                $imputations['ID_CG']   = $ligneDA['ID_CG']??'';
                                $imputations['CODGES']  = $ligneDA['CODGES']??'';
                                $imputations['LIBGES']  = $ligneDA['LIBGES']??'';
                            }
                        }
                        break;
                    case '':
                        $imputations[$axesImputation] = '';
                        break;
                }

                if (($imputations[$axesImputation]??'') != '') {
                    break;
                }
            }
        }
        
        return $imputations;
    }
}
