<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../admin/auth.php';
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['success' => false, 'error' => 'Method not allowed']);
    exit;
}

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

if (empty($input['id'])) {
    http_response_code(400);
    echo json_encode(['success' => false, 'error' => 'Tier ID is required']);
    exit;
}

$tiers = readJSON(SPONSORSHIP_FILE);
$originalCount = count($tiers);
$tiers = array_values(array_filter($tiers, fn($t) => $t['id'] !== $input['id']));

if (count($tiers) === $originalCount) {
    http_response_code(404);
    echo json_encode(['success' => false, 'error' => 'Tier not found']);
    exit;
}

writeJSON(SPONSORSHIP_FILE, $tiers);
echo json_encode(['success' => true, 'deleted_id' => $input['id'], 'remaining' => count($tiers)]);
exit;
