<?php
declare(strict_types=1);

// 🔍 Debug (can remove later)
error_log('MODULES AUTH HEADER (raw): ' . ($_SERVER['HTTP_AUTHORIZATION'] ?? 'MISSING'));

$headers = getallheaders();
error_log('ALL HEADERS: ' . json_encode($headers));

// 🔐 X-Authorization → Authorization bridge
if (
    empty($_SERVER['HTTP_AUTHORIZATION']) &&
    !empty($headers['X-Authorization'])
) {
    $_SERVER['HTTP_AUTHORIZATION'] = $headers['X-Authorization'];
}

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

$actor = require_auth($pdo);

$companyId = (int)$actor['company_id'];

$knownModules = ['USERS', 'AUDIT'];

$modules = [];
foreach ($knownModules as $key) {
    $modules[] = [
        'module_key' => $key,
        'enabled'    => isModuleEnabled($pdo, $companyId, $key)
    ];
}

success('Company modules loaded', [
    'modules' => $modules
]);
