<?php

namespace App\Controller;

use App\Container;
use App\Router\Request\IRequest;
use App\Router\Response\Response;
use Shared\Attributes\Route;
use Twig\Environment;
use Data\Core\Preferences;
use Shared\Log;

class AuthController extends BaseController
{
    
    protected Preferences $preference;

    public function __construct(){
        $this->preference = new Preferences();
    }

    #[Route('/login', name: 'login')]
    public function login(?string $username, ?string $password ,IRequest $request): Response {
        // if user is already logged in, don't display the login page again
        if ($user) {
            return $this->redirectToRoute('blog_index');
        }

        // this statement solves an edge-case: if you change the locale in the login
        // page, after a successful login you are redirected to a page in the previous
        // locale. This code regenerates the referrer URL whenever the login page is
        // browsed, to ensure that its locale is always the current one.
        $this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_index'));

        return $this->render('security/login.html.twig', [
            // last username entered by the user (if any)
            'last_username' => $helper->getLastUsername(),
            // last authentication error (if any)
            'error' => $helper->getLastAuthenticationError(),
        ]);
    }

    // #[Route('/login', name: 'login')]
    // public function login(?string $username, ?string $password ,IRequest $request): Response {
    //     // if user is already logged in, don't display the login page again
    //     if ($user) {
    //         return $this->redirectToRoute('blog_index');
    //     }

    //     // this statement solves an edge-case: if you change the locale in the login
    //     // page, after a successful login you are redirected to a page in the previous
    //     // locale. This code regenerates the referrer URL whenever the login page is
    //     // browsed, to ensure that its locale is always the current one.
    //     $this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_index'));

    //     return $this->render('security/login.html.twig', [
    //         // last username entered by the user (if any)
    //         'last_username' => $helper->getLastUsername(),
    //         // last authentication error (if any)
    //         'error' => $helper->getLastAuthenticationError(),
    //     ]);
    // }

    #[Route(path: '/register', name: 'register', methods: ['POST'])]
    public function register(string $username,string $mdp,string $confirmMdp,string $nom,string $prenom,string $dateNaissance,string $sexe,string $taille,string $poids, IRequest $req): Response
    {
        
       // CONFIRMER LES DONNESS !!!!! IMPORTANT

        return $this->render('./page/home.html.twig',[
            'css' => $this->preference->getCookie()
        ]);
    }

    #[Route(path: '/regist', name: 'regist', methods: ['GET'])]
    public function register2(): Response
    {
        
       // CONFIRMER LES DONNESS !!!!! IMPORTANT

        return $this->render('./page/register.html.twig',[
            'css' => $this->preference->getCookie()
        ]);
    }

    
    function inscription() {
        $model = new ModelVisitor();
        $log=Validation::clean_string($_POST['pseudo']);
        $mdp=Validation::clean_string($_POST['password']);
        if($model->createAUser($log,$mdp)){
            if(ModelUser::login($log, $mdp)){
                UserControler::displayView();
            }
        }
    }
    function connexion(){
        $model = new ModelVisitor();
        if(!isset($_POST['pseudo']) || !isset($_POST['password'])) throw new Exception(" some wrong with credentials !!!!!");
        $log=Validation::clean_string($_POST['pseudo']);
        $mdp=Validation::clean_string($_POST['password']);
            if(ModelUser::login($log, $mdp)){
                UserControler::displayView();
            }
        
    }

    #[Route(path: '/pass', name: 'pass', methods: ['GET'])]
    public function pass(): Response
    {
        
       // CONFIRMER LES DONNESS !!!!! IMPORTANT

        return $this->render('./page/password.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => "Doe",
            'role' => "Athlète",
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => []
        ]);
    }

    #[Route(path: '/log', name: 'log', methods: ['GET'])]
    public function login2(): Response
    {
        
       // CONFIRMER LES DONNESS !!!!! IMPORTANT

        return $this->render('./page/login.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => "Doe",
            'role' => "Athlète",
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => []
        ]);
    }

    #[Route(path: '/password', name: 'password', methods: ['POST'])]
    public function password(string $email, IRequest $req): Response
    {
        
       // CONFIRMER LES DONNESS !!!!! IMPORTANT

        return $this->render('./page/login.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => "Doe",
            'role' => "Athlète",
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => []
        ]);
    }

    
}

?>