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

// Define the path to the JSON file
$jsonFilePath = '../data.json';

// Get the input data
$input = json_decode(file_get_contents('php://input'), true);
$userId = $input['user_id'] ?? null;

// Validate input
if (!$userId) {
    echo json_encode(['success' => false, 'message' => 'No user ID provided']);
    exit;
}

// Read current data
if (!file_exists($jsonFilePath)) {
    echo json_encode(['success' => false, 'message' => 'Data file not found']);
    exit;
}

$jsonContent = file_get_contents($jsonFilePath);
$data = json_decode($jsonContent, true);

// Remove the user if exists
if (isset($data[$userId])) {
    unset($data[$userId]);
    
    // Save back to file
    if (file_put_contents($jsonFilePath, json_encode($data, JSON_PRETTY_PRINT))) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Failed to save data']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'User not found']);
}