<?php
require_once '../config/config.php';
header('Content-Type: application/json; charset=utf-8');
requireAuthentication();

try {
    $input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
    $userId = isset($input['user_id']) ? intval($input['user_id']) : 0;
    $enabled = isset($input['enabled']) ? (int)$input['enabled'] : null;

    if (!$userId || $enabled === null) {
        http_response_code(400);
        echo json_encode(['success' => false, 'error' => 'user_id and enabled required']);
        exit;
    }

    $db = Database::getInstance();
    $db->update('users', ['bot_enabled' => $enabled], 'id = :id', ['id' => $userId]);

    echo json_encode(['success' => true]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
