<?php
namespace Controleur;

use Model\QuoteModel;
use Model\CommentaryModel;
use Gateway\Connection;
use Gateway\QuoteGateway;
use Gateway\CommentaryGateway;
use Model\SearchModel;
use Gateway\UserGateway;
use Model\UserModel;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Gateway\AccueilGateway;

Class VisitorControler {

    private QuoteModel $qMod;
    private CommentaryModel $cMod;
    private AccueilGateway $accueilGateway;

    private SearchModel $sMod;
    
    private UserGateway $gw;

    private UserModel $mdl;

    private array $errors;

    /**
     * @throws SyntaxError
     * @throws RuntimeError
     * @throws LoaderError
     */
    public function __construct() {
        global $co;
        $this->qMod = new QuoteModel(new QuoteGateway($co));
        $this->cMod = new CommentaryModel(new CommentaryGateway($co));
        $this->sMod = new SearchModel(new QuoteGateway($co));

        $this->accueilGateway = new AccueilGateway($co);
        $this->gw = new UserGateway($co);
        $this->mdl = new UserModel($this->gw);
    }

    public function accueil(){
        global $vues;

        // Récupérer la citation du jour via AccueilGateway
        $citationDuJour = $this->accueilGateway->getQuoteOfTheDay('fr');
        $suggestions = $this->accueilGateway->getSuggestions(0, 'fr');

        // Passer les données à la vue
        require_once $vues['accueil'];
    }

    public function quote(array $arg){
        global $vues;
        $id= $arg['idQuote'] ?? 1;
        $q = $this->qMod->searchId($id);
        $c = $this->cMod->getComment($id);
        require_once $vues['quote'];
    }

    public function favorite() {
        global $vues;
        require_once $vues['favorite'];
    }

    public function search(array $arg){
        global $vues;

        $type = ($_POST['type'] ?? "");
        $search = ($_POST['search'] ?? NULL);
        $filtre = ($arg['filtre'] ?? []);


        $tq=$this->sMod->searchQuote($type,$search,$filtre);

        require_once $vues['search'];
    }

    /**
     * @throws SyntaxError
     * @throws RuntimeError
     * @throws LoaderError
     */
    public function login()
    {
        global $vues;
        require_once $vues['login'];

        // global $twig;

        // echo $twig->render("login.html.twig");

        //$this -> toLogIn();
    }

    /**
     * @throws SyntaxError
     * @throws RuntimeError
     * @throws LoaderError
     */
    public function signin(): void
    {
        global $vues;
        require_once $vues['signin'];        
    }





    public function validlogin() : void
    {
        global $vues;
        if ($_POST)
        {
            $pseudo = $_POST['pseudo'] ?? null;
            $mdp = $_POST['mdp'] ?? null;

            $user = $this -> mdl -> getUsername($pseudo);

            if ($user)
            {
                if (password_verify($mdp, $user->getPassword()))
                {
                    $_SESSION['user'] = $pseudo;
                    $_SESSION['role'] = 'user';
                    header("Location: /~kekentin/WF/WF-Website/");
                    exit();
                }else
                {
                    global $twig;
                    $errors = "Identifiant ou mot de passe incorrect";
                    require_once $vues['login'];
                    exit();
                }
            }
            else
            {
                global $twig;
                $errors = "Identifiant ou mot de passe incorrect";
                require_once $vues['login'];
                exit();
            }
        }

    }
    /**
     * @throws RuntimeError
     * @throws SyntaxError
     * @throws LoaderError
     */
    public function validsignin() : void
    {
        global $vues;

        if ($_POST) {
            $pseudo = $_POST['pseudo'] ?? null;
            $email = $_POST['email'] ?? null;
            $mdp = $_POST['mdp'] ?? null;
            $cmdp = $_POST['cmdp'] ?? null;

            if ($mdp != $cmdp) {
                $errors[2] = "Mots de passe incorrects";
                
                require_once $vues['signin'];   
                exit();
            }
            $option = ['cost' => 12];
            $hmdp = password_hash($mdp, PASSWORD_BCRYPT, $option);

            $isUserAlreadyUsed = $this -> mdl -> getUsername($pseudo);
            $isEmailAlreadyUsed = $this -> mdl -> getEmail($email);

            if ($isUserAlreadyUsed and !$isEmailAlreadyUsed)  {
                $errors[0] = "Pseudo déjà utilisé";
                require_once $vues['signin'];   
                exit();
            }
            else if ($isEmailAlreadyUsed and !$isUserAlreadyUsed)  {
                $errors[1] = "Email déjà utilisé";
                require_once $vues['signin'];   
                exit();
            }
            else if ($isEmailAlreadyUsed and $isUserAlreadyUsed)  {
                $errors[0] = "Pseudo déjà utilisé";
                $errors[1] = "Email déjà utilisé";
                require_once $vues['signin'];   
                exit();
            }
            else echo $this->mdl->insertUser($pseudo, $email, $hmdp);

            $_SESSION["role"] = 'user';
            $_SESSION["user"] = $pseudo;

            header("Location: /~kekentin/WF/WF-Website/profil");
        }
    }
}