<?php

use App\Core\Database;

function has_conference_role(int $conference_id, string $role_code): bool
{
    if (is_superadmin()) return true;

    $db = Database::getInstance();
    $user_id = $_SESSION['user']['id'] ?? null;

    if (!$user_id) return false;

    $stmt = $db->prepare("
        SELECT id FROM conference_user_roles
        WHERE conference_id = ?
        AND user_id = ?
        AND role_code = ?
    ");

    $stmt->execute([$conference_id, $user_id, $role_code]);

    return (bool)$stmt->fetch();
}

function require_conference_role(int $conference_id, string $role_code): void
{
    if (!has_conference_role($conference_id, $role_code)) {
        http_response_code(403);
        die("Access denied for this conference role.");
    }
}
