<?php
class PortalAuth
{
    public static function check(): bool
    {
        return !empty($_SESSION['portal_user_id']);
    }

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

    public static function login(array $user): void
    {
        session_regenerate_id(true);
        $_SESSION['portal_user_id'] = $user['id'];
        $_SESSION['portal_user'] = $user;
    }

    public static function logout(): void
    {
        unset($_SESSION['portal_user_id'], $_SESSION['portal_user']);
        session_regenerate_id(true);
    }

    public static function guard(): void
    {
        if (!self::check()) {
            $config = require __DIR__ . '/../../config/config.php';
            $baseUrl = rtrim($config['app']['url'] ?? '', '/');
            header('Location: ' . $baseUrl . '/portal/login');
            exit;
        }
    }
}
