<?php
require_once '../config/database.php';
require_once '../includes/functions.php';

// Check if admin is logged in
if (!isAdminLoggedIn()) {
    http_response_code(403);
    echo json_encode(['error' => 'Unauthorized']);
    exit();
}

$conn = getConnection();
$countryId = intval($_GET['country_id'] ?? 0);

if ($countryId <= 0) {
    echo json_encode([]);
    exit();
}

// Get tiers for the country
$stmt = $conn->prepare("SELECT * FROM withdrawal_fee_tiers WHERE country_id = ? ORDER BY min_amount ASC");
$stmt->bind_param("i", $countryId);
$stmt->execute();
$result = $stmt->get_result();

$tiers = [];
while ($row = $result->fetch_assoc()) {
    $tiers[] = [
        'id' => $row['id'],
        'min_amount' => $row['min_amount'],
        'max_amount' => $row['max_amount'],
        'fee' => $row['fee']
    ];
}

header('Content-Type: application/json');
echo json_encode($tiers);
?>