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

require_once __DIR__ . '/../includes/auth.php'; // keep this path

$perm = $_GET['perm'] ?? '';
http_response_code(403);

/*
|--------------------------------------------------------------------------
| Smart Dashboard Redirect Based On Permission
|--------------------------------------------------------------------------
*/

$dashboardLink = 'dashboard.php'; // default fallback

if (function_exists('hasPermission')) {

    // Installer dashboard
    if (hasPermission('installer_portal')) {
        $dashboardLink = 'installers/dashboard.php';
    }

    // Operations dashboard
    elseif (hasPermission('view_operations')) {
        $dashboardLink = 'operations/index.php';
    }

    // National Network dashboard
    elseif (hasPermission('manage_network_jobs') || hasPermission('view_network_jobs')) {
        $dashboardLink = 'network/index.php';
    }
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>403 - Access Denied</title>
  <link rel="stylesheet" href="assets/style.css">
</head>
<body>

  <div style="max-width:720px;margin:60px auto;padding:24px;">
    <h2>403 — Access Denied</h2>

    <p>
      You don’t have permission to access this module
      <?= $perm ? "(<strong>" . htmlspecialchars($perm) . "</strong>)" : "" ?>.
    </p>

    <a href="<?= $dashboardLink ?>"
       style="display:inline-block;margin-top:18px;">
       ← Back to Dashboard
    </a>

  </div>

</body>
</html>