<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!function_exists('is_logged_in')) {
    function is_logged_in(): bool {
        return isset($_SESSION['user']);
    }
}

if (!function_exists('current_user')) {
    function current_user(): ?array {
        return $_SESSION['user'] ?? null;
    }
}

if (!function_exists('require_login')) {
    function require_login(): void {
        if (!is_logged_in()) {
            header("Location: login.php");
            exit;
        }
    }
}

if (!function_exists('require_role')) {
    function require_role(array $roles): void {
        require_login();
        $user = current_user();
        if (!$user || !in_array($user['role'], $roles, true)) {
            die("Bu sayfaya erişim yetkiniz yok.");
        }
    }
}
