<?php
declare(strict_types=1);

require_once __DIR__ . '/../system/bootstrap.php';
require_once __DIR__ . '/../system/auth.php';
require_once __DIR__ . '/../system/response.php';

header('Content-Type: application/json');

/* =========================
   AUTH
   ========================= */
$actor = require_auth($pdo);

/*
 Only allow admin-level roles to list roles
 Adjust if needed later
*/
if (!in_array($actor['role_key'], [
    'super_admin',
    'support',
    'rsp_owner',
    'rsp_admin',
    'company_admin'
], true)) {
    error_forbidden('Forbidden');
}

/* =========================
   FETCH ROLES
   ========================= */

$stmt = $pdo->prepare("
    SELECT
        id,
        role_key,
        rank
    FROM roles
    ORDER BY rank ASC
");

$stmt->execute();

$roles = $stmt->fetchAll(PDO::FETCH_ASSOC);

/* =========================
   RESPONSE
   ========================= */

success('Roles loaded', [
    'roles' => $roles
]);
