<?php

namespace model;

use DAL\AdminGateway;
use DAL\Connection;
use metier\Admin;

class AdminModel
{
    /**
     * @throws \Exception
     */
    public function connection (string $username, string $mdp){
        //Validation::validationLogin($username);
        //Validation::validationMdp($mdp);

        $gwArticle = new AdminGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto','rorossetto','tpphp'));
        $lmdp = $gwArticle->login($username);

        foreach ($lmdp as $motDePasse){
            if (true){//password_verify($mdp,$motDePasse['password']) or $mdp == $motDePasse['password']){
                $_SESSION['role'] = 'admin';
                $_SESSION['pseudo'] = $username;
                return new Admin($username,$motDePasse['mail']);
            }
        }
        return null;
    }

    public static function isAdmin(): bool
    {
        return (isset($_SESSION['role']) && $_SESSION['role'] == 'admin');
    }

    public static function deconnection(){
        $_SESSION['role'] = "";
        unset($_SESSION['role']);
        $_SESSION['pseudo'] = "";
        unset($_SESSION['pseudo']);
        header("Location: /~mapoint2/SAE/Php_RSS/fluxRSS/admin");
    }
}