<?php
use Helpers\Outil\Sql;
use Skeleton\Application;

use function DI\env;

$loader = require_once '../vendor/autoload.php';

// error_reporting(E_ALL & ~E_DEPRECATED & ~E_WARNING);

$projectRoot = Application::getAppRoot();

/**
 * Initialisation de la Session
 */

\Helpers\Session\Session::initSessionActive();
unset($_SESSION['Session_NumClient']);

$horsConnexion = 0;

$devSession_NumClient = 0;

/** Faux positif */
/** @phpstan-ignore-next-line */
if ($devSession_NumClient) {
    $_SESSION['Session_NumClient'] = $devSession_NumClient;
}

if (!isset($_SESSION['Session_NumClient'])) {
    $horsConnexion = 1;

    $builderInit = new DI\ContainerBuilder();

    $builderInit->addDefinitions($projectRoot . '\config\configSessionInit.php');
    $containerSession              = $builderInit->build();
    $_SESSION['Session_NumClient'] = Sql::getNumClient($containerSession->get(\PDO::class));
    /** @phpstan-ignore-next-line */
    //setVersionArrayV2($containerSession->get(\PDO::class));
}


if (!isset($_SESSION['Session_Langue']) && ($_SESSION['connexionModeSSO'] ?? 0) != 1
    //  || (array_key_exists('OxaprocDeflangue', $_COOKIE) && $_COOKIE['OxaprocDeflangue'])
) {
    if (array_key_exists('OxaprocDeflangue', $_COOKIE) && $_COOKIE['OxaprocDeflangue']) {
        $_SESSION['Session_Langue'] = $_COOKIE['OxaprocDeflangue'];
    } else {
        if (!isset($containerSession)) {
            $builderInit = new DI\ContainerBuilder();
            $builderInit->addDefinitions($projectRoot . '\config\configSessionInit.php');
            $containerSession              = $builderInit->build();
            $_SESSION['Session_NumClient'] = Sql::getNumClient($containerSession->get(\PDO::class));
            /** @phpstan-ignore-next-line */
            //setVersionArrayV2($containerSession->get(\PDO::class));
        }
        $_SESSION['Session_Langue'] = Sql::boolSql(Sql::paravrac($containerSession->get(\PDO::class), 'PARAMOXA', 'MULTILNGACTIF')) ? \Helpers\Outil\Roles::droit($containerSession->get(\PDO::class), 'LNGBDD', 'PARAMOXA', true) : '';
    }
}

/**
 * Recuperation et le merge des differents Modules standart et Specifique client
 */
$builder   = new DI\ContainerBuilder();
$modules   = \Skeleton\Application::builderIndex(
    $builder,
    $projectRoot . '\config\configSkeleton.php',
    $projectRoot . '\config\configVersion.php',
    require $projectRoot . '/config/Module/configModule.php',
    require $projectRoot . '/config/Specifique/configModuleSpecifique.php',
    require $projectRoot . '/config/ModuleExterne/configModuleExterne.php',
    (getenv('TYPEENVIRONNEMENT') == 'recette') ? require $projectRoot . '/config/ModuleTest/configModuleTest.php' : []
);
$container = $builder->build();

/**
 * Controlle de la securité
 */
\Helpers\Securite\Securite::securityInit($container, $projectRoot . '\config\configSessionInit.php');

/** @phpstan-ignore-next-line */
//$version = getVersionProduit();

/** Faux positif */
/** @phpstan-ignore-next-line */
if ($devSession_NumClient) {
    $_SESSION['Session_NumClient'] = $devSession_NumClient;
}

$application = new \Skeleton\Application($container, $modules);
$response    = $application->run($container->get(\Psr\Http\Message\ServerRequestInterface::class));

\Http\Response\send($response);
