<?php

namespace Modules\Utilisateur\Service;

use Entity\Model\Hastutil;
use Entity\Model\OxaUserPanier;
use Entity\Model\OxaUsers;
use Entity\Model\UsrBack;
use Helpers\Outil\EnvHelper;
use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;
use Modules\Connexion\Service\InitialisationSessionUser;
use Psr\Container\ContainerInterface;
use Modules\Index\Outil\OutilConnexion;
use Modules\Panier\Service\OutilPanierService;
use Skeleton\Application;

class BackUpService
{
    private \PDO $pdoOxaFT;
    private \PDO $pdo;

    private string $userOrigine;
    private string $langue;

    public function __construct(\PDO $pdo, \PDO $pdoOxaFT, string $userOrigine)
    {
        $this->pdo = $pdo;
        $this->pdoOxaFT = $pdoOxaFT;

        $this->userOrigine = $userOrigine;
        $this->langue = $_SESSION['Session_Langue'];
    }

    public function initSession():void
    {
        session_destroy();

        session_name('__OxaProc-PHPSESSID');
        session_start();
        $_SESSION['stats_SQLServer'] = 0;

        $projectRoot = Application::getAppRoot();
        $builderInit = new \DI\ContainerBuilder();
        $builderInit->addDefinitions($projectRoot . '\config\configSessionInit.php');
        $containerSession = $builderInit->build();

        $_SESSION['Session_NumClient'] = Sql::getNumClient($containerSession->get(\PDO::class));

        (new EnvHelper())->getEnv();
        /** @phpstan-ignore-next-line */
        //setVersionArrayV2($containerSession->get(\PDO::class));
        \Helpers\Securite\Securite::securityInit($containerSession, $projectRoot . '\config\configSessionInit.php');

        OutilPanierService::checkRechercheActif($this->pdoOxaFT);
    }

    public function changeBackup(ContainerInterface $contenaire, string $idUser):void
    {
        //check si le backup est possible.

        $userBackup = (new UsrBack($this->pdo))->find(['COD_USR' => $idUser, 'COD_BAC' => $this->userOrigine]);
        
        $modeMultiLng = sql::paravrac($this->pdo, 'PARAMOXA', 'MULTILNGACTIF') == 'O';
        $_SESSION['user_is_logged']     = true;
        $_SESSION['MODE_BACKUP']        = true;
        $_SESSION['user_origine']       = $this->userOrigine;
        $_SESSION['code_user']          = $idUser;
        $_SESSION['TYPE_BACKUP_ACHAT']  = sql::boolSql($userBackup['ACHAT']);
        $_SESSION['TYPE_BACKUP_RESP']   = sql::boolSql($userBackup['SUPHIER']);
        $_SESSION['Session_Langue']     = $modeMultiLng? $this->langue:'';

        $this->initSessionSociete($idUser);

        $this->initSessionByWsdl($contenaire, $idUser);
    }

    public function deconnexioBackUp(ContainerInterface $contenaire) :void
    {
        $modeMultiLng   = sql::paravrac($this->pdo, 'PARAMOXA', 'MULTILNGACTIF') == 'O';

        $_SESSION['user_is_logged']             = true;
        $_SESSION['Session_Langue']             = $modeMultiLng? $this->langue : '';

        $this->initSessionSociete($this->userOrigine);
        $this->initSessionByWsdl($contenaire, $this->userOrigine);
    }

    private function initSessionSociete(string $idUser):void
    {
        $oxaUsers = (new OxaUsers($this->pdo))->find(['login' => $idUser]);

        $oxaUsersPanier = false;
        if ($oxaUsers) {
            $oxaUsersPanier = (new OxaUserPanier($this->pdo))->find(['id_user' => $oxaUsers['id_user'], 'id_panier' => '0']);
            if ($oxaUsersPanier) {
                $_SESSION['Session_SocUsr'] = $oxaUsersPanier['CODE_STE'];
            }
        }
        
        if (!isset($_SESSION['Session_SocUsr'])) {
            $userStd = (new Hastutil($this->pdo))->find($idUser);
            $_SESSION['Session_SocUsr'] = $userStd['CODE_STE'];
        }
    }

    private function initSessionByWsdl(ContainerInterface $contenaire, string $user):void
    {
        (new InitialisationSessionUser($this->pdo, $this->pdoOxaFT, $contenaire->get(ITrad::class)))->initSessionUser($user, $this->langue);

        $connexionOutil = new OutilConnexion($this->pdo, $this->pdoOxaFT, $contenaire);
        $connexionOutil->initParametrage($this->pdo);
    }
}
