<?php
namespace Skeleton\Controller;

use Helpers\Log\LogSysteme;

use Helpers\Securite\Securite;
use Helpers\Securite\SecuriteRoute;
use Helpers\Session\ParseBase1;
use Helpers\Trad\TradDev;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Skeleton\Controller\Traits\Response;
use Skeleton\Controller\Traits\ControllerShorthand;
use Psr\Container\ContainerInterface;
use Skeleton\Renderer\RendererInterface;
use Skeleton\Request;
use Skeleton\Router;

abstract class AbstractController
{
    use Response;
    use ControllerShorthand;

    protected ContainerInterface $contenaire;
    protected Request $request;
    protected Router $router;
    protected RendererInterface $renderer;

    protected string $JWTKEY = 'fSS!fsf545f-sd5sf$0ezegfrs?df0Hf-5f04zerzerdf45$sdfsd6t5rP-r6teg4fd0?s470f.fzezfrsdf';

    protected string $idSecretMobil = '99c63f559b95a713fd8fff3bcf76ae4800d8b3d4fece320a3c8a5e409767eac7';
    
    protected string $idNumConcatfichier = '786585sdfsdfgedgdhest545194465zerilfqsf';
    
    protected string $preRequisMobilityVersion = '1.8.1';
    
    private \Helpers\Log\LogSysteme $LogSysteme;

    public function __construct()
    {
        /**
         * Mise en place du compteur de requete
         */
        if ((new ParseBase1())->getDebug()) {
            $this->LogSysteme = new LogSysteme(2);
            
            if (isset($_SESSION['stats_SQLServer'])) {
                $this->LogSysteme->setContentInLog($_SESSION['stats_SQLServer']);
            }
            $_SESSION['stats_SQLServer'] = 0;
        }
    }
    
    /**
     * __invoke
     *
     * @param  ServerRequestInterface $request
     * @return ResponseInterface|string|false
     */
    public function __invoke(ServerRequestInterface $request)
    {
        if (isset($_SESSION['trashV1'])) {
            unset($_SESSION['trashV1']);
        }
        
        $post = (array) $request->getParsedBody();
        $this->request = new \Skeleton\Request($request, $this->router);
        
        /**
         * Init des session et des params
         */
        \Helpers\ExportTo\Outils::initSession($post);
        \Helpers\Trad\Trad::initSession();
        \Helpers\Session\Session::initSession();
        \Helpers\Filter\FiltreAvance\Generator::initSession();
        \Helpers\Pagination\Paginator::initSession($post);
        
        $path = $this->router->getNameRoute();
        
        /**
         * Gestion de la securité CSP
         */
        
         Securite::setHeaderContentSecurityPolicyReportOnly($this->router->generateUri('Index.Index.cspRapport'));
        
          /**
         * Vérification de la session et des routes publiques
         */

        if (empty($_SESSION['Session_Login']) &&
            !array_key_exists($path, $this->router->getPublicRoutes()) &&
            empty($_SESSION['batch'])
        ) {
            return $this->redirect('Connexion.Connexion.connexion');
        }

        /**
         * Gestion des action via le path
         */

        $action = explode(".", $path);

        /**
         * Gestion des droits global via le Module/Controller/Action
         */
        if (\Helpers\Securite\SecuriteRoute::initSecurityRules($action[2], $this, $this->contenaire) == false) {
            if (method_exists($this, 'errorRules')) {
                return $this->errorRules();
            }
            return $this->redirect('Index.Index.errorDroit');
        }
        
        $chargement = false;
        if (method_exists($this, 'init')) {
            // @phpstan-ignore-next-line
            $response = $this->contenaire->call([$this, 'init']);
            if ($response instanceof ResponseInterface) {
                return $init =  $response;
            }
        }
        // @phpstan-ignore-next-line
        $action = $this->contenaire->call([$this, $action[2] . 'Action']);

        $init = '';
        if (method_exists($this, 'pageInit')) {
            // @phpstan-ignore-next-line
            $response = $this->contenaire->call([$this, 'pageInit']);
            
            if ($response instanceof ResponseInterface) {
                $chargement = true;
                $init =  $response;
            }
        }
        
        if ($chargement) {
            return $init;
        } else {
            return $action;
        }
    }

    abstract public function getPdo() :\PDO;
}
