<?php
/**
 * BOAFO Auth
 * Session-based authentication helper
 */
class Auth {
    public static function check(): bool {
        return !empty($_SESSION['user']);
    }

    public static function user(): ?array {
        return $_SESSION['user'] ?? null;
    }

    public static function id(): ?int {
        return $_SESSION['user']['id'] ?? null;
    }

    public static function role(): ?string {
        return $_SESSION['user']['role'] ?? null;
    }

    public static function isAdmin(): bool {
        return self::role() === ROLE_ADMIN;
    }

    public static function isStudent(): bool {
        return self::role() === ROLE_STUDENT;
    }

    public static function isMentor(): bool {
        return self::role() === ROLE_MENTOR;
    }

    public static function isTutor(): bool {
        return self::role() === ROLE_TUTOR;
    }

    /**
     * Login: regenerate session to prevent fixation
     */
    public static function login(array $user): void {
        session_regenerate_id(true);
        $_SESSION['user'] = [
            'id'       => $user['id'],
            'uuid'     => $user['uuid'],
            'name'     => $user['first_name'] . ' ' . $user['last_name'],
            'email'    => $user['email'],
            'role'     => $user['role'],
            'avatar'   => $user['avatar'],
            'status'   => $user['status'],
        ];
    }

    public static function logout(): void {
        $_SESSION = [];
        session_destroy();
        session_start();
        session_regenerate_id(true);
    }
}
