<?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');

$user = require_auth($pdo);

$role = strtolower((string)$user['role_key']);

if (!in_array($role, [
    'super_admin',
    'platform_admin',
    'rsp_admin',
    'rsp_owner',
    'company_admin'
], true)) {
    error_forbidden('Forbidden');
}

/* =========================
   QUERY
   ========================= */

$stmt = $pdo->prepare("
SELECT
    s.id,
    s.name,
    s.department_id,
    d.name AS department_name
FROM sections s
LEFT JOIN departments d
    ON d.id = s.department_id
ORDER BY d.name, s.name
");

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

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

success('Sections loaded',$rows);