<?php
// pages/components/toggle_module_status.php
// HTMX endpoint to toggle a module's _STATUS configuration (session-backed demo)

if (!defined('BEEZUI_ROOT')) {
    define('BEEZUI_ROOT', dirname(__DIR__, 2)); // pages/components => root
}

$module = mh_get('module', '', 'key');
if ($module === '') {
    http_response_code(400);
    echo '<div class="text-sm text-red-600">Missing module key</div>';
    return;
}

// Determine desired state from POST: checkbox named "status"
$on = mh_post('status', null) !== null;
if ($on && !ConfigStore::isConfigured($module)) {
    // Ignore activation if not configured
} else {
    if (!$on) {
        ConfigStore::setConfigured($module);
    }
    ConfigStore::setModuleStatus($module, $on);
}

// Load module data and render the full card so the client can swap the entire module container
$all = ModuleLoader::all();
$m = $all[$module] ?? null;
if (!$m) {
    http_response_code(404);
    echo '<div class="text-sm text-red-600">Unknown module: ' . Base::h($module) . '</div>';
    return;
}

include __DIR__ . '/trigger_module.php';
