<?php
// app/cnx/professeur/AuthHelper.php

namespace app\cnx\professeur;

require_once __DIR__ . '/SessionManager.php';

class AuthHelper
{
    public static function requireLogin()
    {
        $session = SessionManager::getInstance();

        if (!$session->isLoggedIn()) {

            header('Location: /est-concours/app/cnx/employee/index.php');
            exit();
        }
        return true;
    }

    public static function requireRole($role)
    {
        self::requireLogin();

        $session = SessionManager::getInstance();
        if (!$session->hasRole($role)) {
            header('Location: /est-concours/app/cnx/employee/access-denied.php');
            exit();
        }
        return true;
    }

    public static function requireAnyRole($roles)
    {
        self::requireLogin();

        $session = SessionManager::getInstance();
        if (!$session->hasAnyRole($roles)) {
            header('Location: /est-concours/app/cnx/professeur/access-denied.php');
            exit();
        }
        return true;
    }

    public static function getCurrentUser($redirectIfNotLoggedIn = false)
    {
        $session = SessionManager::getInstance();

        if ($redirectIfNotLoggedIn && !$session->isLoggedIn()) {
            header('Location: /est-concours/app/cnx/professeur/index.php');
            exit();
        }

        return $session->getCurrentUser();
    }

    public static function isLoggedIn()
    {
        $session = SessionManager::getInstance();
        return $session->isLoggedIn();
    }

    public static function getUserId()
    {
        $session = SessionManager::getInstance();
        return $session->getUserId();
    }

    public static function getUserRole()
    {
        $session = SessionManager::getInstance();
        return $session->getUserRole();
    }

    public static function logout()
    {
        $session = SessionManager::getInstance();
        return $session->logout();
    }
}
?>
