<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET");
header("Authorization: Bearer");

require_once '../../config/db.php';
require_once '../../core/Auth.php';

try {
    $user_id = Auth::check(); // Returns user ID from token

    // Get Items
    $stmt = $pdo->prepare("SELECT * FROM chest_items WHERE user_id = ? ORDER BY created_at DESC");
    $stmt->execute([$user_id]);
    $items = $stmt->fetchAll();

    // Get Economy Settings
    $stmt_fee = $pdo->prepare("SELECT setting_value FROM site_config WHERE setting_key = 'chest_item_fee_percent'");
    $stmt_fee->execute();
    $fee = (float)($stmt_fee->fetchColumn() ?: 15);

    echo json_encode([
        'status' => 'success',
        'items' => $items,
        'settings' => [
            'chest_fee' => $fee
        ]
    ]);

} catch (Exception $e) {
    http_response_code(401);
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>
