<?php
// helpers/auth_helper.php

function startSessionIfNeeded() {
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }
}

function enforcePermission($pdo, $requiredPermission, $redirectPage = 'dashboard.php') {
    startSessionIfNeeded();

    require_once __DIR__ . '/../auth/access.php';
    require_once __DIR__ . '/../auth/middleware.php';

    if (!isset($_SESSION['user_id']) || !hasPermission($_SESSION['user_id'], $requiredPermission, $pdo)) {
        denyAccessModal("You are not authorized to access this page.", $redirectPage);
    }
}
