<?php
header('Content-Type: application/json');
require_once dirname(__DIR__) . '/include/scheduler.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['ok' => false, 'error' => 'POST only']);
    exit;
}

$name    = trim($_POST['name']    ?? '');
$enabled = ($_POST['enabled'] ?? '0') === '1';

if (!$name || !preg_match('/^[A-Z_]+_RSYNC_ENABLED$/', $name)) {
    echo json_encode(['ok' => false, 'error' => 'Invalid flag name']);
    exit;
}

$ok = vv_conf_flag_set($name, $enabled);

// master.conf is shared — propagate the change to partner hosts (no-op on non-owner).
$push = [];
if ($ok) {
    $push = vv_push_master_conf();
    vv_push_setup_state();
}

echo json_encode(['ok' => $ok, 'error' => $ok ? null : 'Failed to write master.conf', 'push' => $push]);
