
<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once("definitions.php");

// Only accept POST requests with JSON body
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || empty(file_get_contents('php://input'))) {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => 'Invalid request']);
    exit;
}

$data = json_decode(file_get_contents('php://input'), true);

if (!isset($data['agent_key']) || !is_numeric($data['agent_key'])) {
    http_response_code(400);
    echo json_encode(['success' => false, 'message' => 'Invalid agent key']);
    exit;
}

$agent_key = intval($data['agent_key']);

// Create connection
$conn = new mysqli(PACKAGE_LOG_DB_SERVER, PACKAGE_LOG_DB_USER, PACKAGE_LOG_DB_PASS, PACKAGE_LOG_DB_NAME);
if ($conn->connect_error) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Database connection failed']);
    exit;
}

// Prepare update statement
$stmt = $conn->prepare("UPDATE agent SET deleted = 0, active = 0 WHERE agent_key = ?");
$stmt->bind_param("i", $agent_key);

if ($stmt->execute()) {
    echo json_encode(['success' => true]);
} else {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Database update failed']);
}

$stmt->close();
$conn->close();
?>