<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/functions.php';

function requireUserLogin(): void {
    if (empty($_SESSION['user_id'])) {
        header('Location: /login.php?next=' . urlencode($_SERVER['REQUEST_URI']));
        exit;
    }
}
function requireAdminLogin(): void {
    if (empty($_SESSION['admin_id'])) {
        header('Location: /admin/login.php');
        exit;
    }
}
function isUserLoggedIn(): bool {
    return !empty($_SESSION['user_id']);
}
function isAdminLoggedIn(): bool {
    return !empty($_SESSION['admin_id']);
}
function currentUser(PDO $pdo): ?array {
    if (empty($_SESSION['user_id'])) return null;
    return getUserById($pdo, (int)$_SESSION['user_id']);
}
function currentAdmin(PDO $pdo): ?array {
    if (empty($_SESSION['admin_id'])) return null;
    return getAdminById($pdo, (int)$_SESSION['admin_id']);
}
function adminHasRole(string ...$roles): bool {
    return in_array($_SESSION['admin_role'] ?? '', $roles, true);
}
function requireSuperAdmin(): void {
    if (($_SESSION['admin_role'] ?? '') !== 'super_admin') {
        header('Location: /admin/index.php?error=access_denied');
        exit;
    }
}
