<?php
// Task 19: AJAX endpoint to disable a single site.
// Root-only, CSRF-validated POST.
// Returns JSON: { "ok": true } or { "ok": false, "error": "..." }

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

if (!is_root_user()) {
    http_response_code(403);
    echo json_encode(['ok' => false, 'error' => 'forbidden']);
    exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['ok' => false, 'error' => 'method_not_allowed']);
    exit;
}
if (!validate_csrf_token()) {
    http_response_code(403);
    echo json_encode(['ok' => false, 'error' => 'csrf']);
    exit;
}

require_once('/docker/whp/web/libs/site_disable.php');

$site_id = (int)($_POST['site_id'] ?? 0);
$reason  = (string)($_POST['reason'] ?? '');
$note    = isset($_POST['note']) && $_POST['note'] !== '' ? (string)$_POST['note'] : null;
$actor   = defined('AUTH_USER') ? AUTH_USER : 'unknown';

if ($site_id <= 0) {
    http_response_code(400);
    echo json_encode(['ok' => false, 'error' => 'invalid_site_id']);
    exit;
}

echo json_encode(disable_site($site_id, $reason, $actor, $note));
