<?php
use Helpers\Outil\EnvHelper;
use Helpers\Session\Session;
use Helpers\Trad\Trad;
use Helpers\Outil\Sql;
use Skeleton\Application;

// Polyfill PHP 8
if (!function_exists('str_starts_with')) {
    function str_starts_with(string $haystack, string $needle)
    {
        return empty($needle) || strpos($haystack, $needle) === 0;
    }
}

if (!function_exists("array_is_list")) {
    function array_is_list(array $array): bool
    {
        $i = 0;
        foreach ($array as $k => $v) {
            if ($k !== $i++) {
                return false;
            }
        }
        return true;
    }
}

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


$projectRoot = Application::getAppRoot();

/**
 * Initialisation de la Session
 */

Session::initSessionActive();

/**
 * A supprimer après la disparition de la V1
 */
require_once 'application/webroot/php/include/version.php';

$horsConnexion = 0;

$devSession_NumClient = 0;
if (!isset($_SESSION['Session_NumClient'])) {

    /**
     * A supprimer après la disparition de la V1
     */
    require_once 'application/autoload/Outil/Class_Connecteur.php';
    require_once 'application/autoload/Class_Param.php';
    require_once 'application/config/boot_init.php';
    $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) : '';
    }
}
/** Faux positif */
/** @phpstan-ignore-next-line */
if ($devSession_NumClient != 0) {
    $_SESSION['Session_NumClient'] = $devSession_NumClient;
}



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

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

/** A supprimer quand la V1 disparait */
/** @phpstan-ignore-next-line */
$version = getVersionProduit();

if ($horsConnexion) {
    $_SESSION['Session_NumClient'] = \Helpers\Outil\Roles::droit($container->get(\PDO::class), 'CLIENT_REFERENCE_SPECIF', 'PARAMGEN', true);
}
/** 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);
