<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

require '../../../config/db.php';

try {
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM site_config WHERE setting_key IN ('diamond_exchange_rate', 'min_withdrawal_diamonds', 'withdrawal_fee_percent', 'sales_fee_percent', 'marketplace_category_fees', 'chat_unlock_fee_percent', 'subscription_fee_percent', 'chest_item_fee_percent', 'site_name', 'maintenance_mode')");
    $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
    
    // Default values if missing
    $defaults = [
        'diamond_exchange_rate' => '0.01',
        'min_withdrawal_diamonds' => '100',
        'withdrawal_fee_percent' => '0',
        'sales_fee_percent' => '10',
        'chat_unlock_fee_percent' => '20',
        'subscription_fee_percent' => '25',
        'chest_item_fee_percent' => '15',
        'marketplace_category_fees' => '{}'
    ];

    $response = array_merge($defaults, $settings);

    echo json_encode(['success' => true, 'data' => $response]);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
