<?php
declare(strict_types=1);

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

function requireModule(string $moduleKey): void
{
    // Always authenticate using the real system
    $user = require_auth($GLOBALS['pdo']);

    // Platform admins bypass module enforcement
    if (in_array($user['role_key'], ['super_admin', 'platform_admin'], true)) {
        return;
    }

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

    if (!isModuleEnabled($GLOBALS['pdo'], $companyId, $moduleKey)) {
        error_forbidden('Module disabled');
    }
}
