<?php
$pageTitle = 'My Coins';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: /login');
    exit;
}

// Load site settings
require_once __DIR__ . '/../../models/SiteSettings.php';
$siteSettings = new SiteSettings();
$settings = $siteSettings->getAllSettings();

// Check if points system is enabled
if (($settings['enable_points_system'] ?? '0') !== '1') {
    header('Location: /profile');
    exit;
}

// Load user and coins data
require_once __DIR__ . '/../../models/User.php';
require_once __DIR__ . '/../../models/Coins.php';

$userModel = new User();
$coinsModel = new Coins();

$user = $userModel->getUserWithCoins($_SESSION['user_id']);
$balance = $coinsModel->getUserBalance($_SESSION['user_id']);
$transactions = $coinsModel->getUserTransactions($_SESSION['user_id'], 20);
$withdrawals = $coinsModel->getUserWithdrawalRequests($_SESSION['user_id'], 10);

$customJs = ['/public/js/coins.js'];

include __DIR__ . '/../layout/header.php';
?>

<div class="container mx-auto px-4 py-8">
    <div class="max-w-4xl mx-auto">
        <!-- Header -->
        <div class="mb-8">
            <h1 class="text-3xl font-bold mb-2">My Coins</h1>
            <p class="text-gray-400">Manage your earned coins and withdrawal requests</p>
        </div>

        <!-- Balance Cards -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
            <div class="bg-card-bg rounded-xl p-6 text-center">
                <div class="text-yellow-400 text-3xl mb-2">
                    <i class="fas fa-coins"></i>
                </div>
                <h3 class="text-lg font-semibold mb-1">Current Balance</h3>
                <p class="text-2xl font-bold text-yellow-400"><?= number_format($balance['balance']) ?></p>
                <p class="text-sm text-gray-400">coins</p>
            </div>
            
            <div class="bg-card-bg rounded-xl p-6 text-center">
                <div class="text-green-400 text-3xl mb-2">
                    <i class="fas fa-chart-line"></i>
                </div>
                <h3 class="text-lg font-semibold mb-1">Total Earned</h3>
                <p class="text-2xl font-bold text-green-400"><?= number_format($balance['total_earned']) ?></p>
                <p class="text-sm text-gray-400">coins</p>
            </div>
            
            <div class="bg-card-bg rounded-xl p-6 text-center">
                <div class="text-blue-400 text-3xl mb-2">
                    <i class="fas fa-dollar-sign"></i>
                </div>
                <h3 class="text-lg font-semibold mb-1">USD Value</h3>
                <p class="text-2xl font-bold text-blue-400">$<?= number_format($balance['balance'] / ($settings['withdrawal_rate'] ?? 100), 2) ?></p>
                <p class="text-sm text-gray-400">estimated</p>
            </div>
        </div>

        <!-- Actions -->
        <div class="mb-8">
            <div class="flex flex-wrap gap-4">
                <button onclick="openWithdrawalModal()" class="bg-primary hover:bg-primary-dark text-white px-6 py-3 rounded-lg font-medium">
                    <i class="fas fa-money-bill-wave mr-2"></i>
                    Request Withdrawal
                </button>
                <a href="/profile" class="bg-gray-700 hover:bg-gray-600 text-white px-6 py-3 rounded-lg font-medium">
                    <i class="fas fa-user mr-2"></i>
                    Back to Profile
                </a>
            </div>
        </div>

        <!-- Withdrawal Info -->
        <div class="bg-blue-900/20 border border-blue-600 rounded-lg p-4 mb-8">
            <h3 class="font-semibold text-blue-400 mb-2">
                <i class="fas fa-info-circle mr-2"></i>
                Withdrawal Information
            </h3>
            <ul class="text-sm text-gray-300 space-y-1">
                <li>• Minimum withdrawal: <?= number_format($settings['minimum_withdrawal'] ?? 1000) ?> coins</li>
                <li>• Exchange rate: <?= number_format($settings['withdrawal_rate'] ?? 100) ?> coins = $1 USD</li>
                <li>• Processing time: 1-3 business days</li>
                <li>• Supported payment methods: <?= htmlspecialchars($settings['withdrawal_methods'] ?? 'PayPal, Bank Transfer, Cryptocurrency') ?></li>
            </ul>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
            <!-- Recent Transactions -->
            <div class="bg-card-bg rounded-xl p-6">
                <h2 class="text-xl font-semibold mb-4">Recent Transactions</h2>
                
                <?php if (empty($transactions)): ?>
                    <div class="text-center py-8">
                        <i class="fas fa-history text-4xl text-gray-600 mb-4"></i>
                        <p class="text-gray-400">No transactions yet</p>
                        <p class="text-sm text-gray-500">Start playing games to earn coins!</p>
                    </div>
                <?php else: ?>
                    <div class="space-y-3">
                        <?php foreach ($transactions as $transaction): ?>
                            <div class="flex items-center justify-between p-3 bg-darker rounded-lg">
                                <div class="flex items-center">
                                    <div class="w-8 h-8 rounded-full flex items-center justify-center mr-3 <?= $transaction['type'] === 'earned' ? 'bg-green-600' : ($transaction['type'] === 'spent' ? 'bg-red-600' : 'bg-blue-600') ?>">
                                        <i class="fas <?= $transaction['type'] === 'earned' ? 'fa-plus' : ($transaction['type'] === 'spent' ? 'fa-minus' : 'fa-undo') ?> text-xs"></i>
                                    </div>
                                    <div>
                                        <p class="font-medium"><?= htmlspecialchars($transaction['description']) ?></p>
                                        <?php if ($transaction['game_title']): ?>
                                            <p class="text-sm text-gray-400"><?= htmlspecialchars($transaction['game_title']) ?></p>
                                        <?php endif; ?>
                                        <p class="text-xs text-gray-500"><?= date('M j, Y g:i A', strtotime($transaction['created_at'])) ?></p>
                                    </div>
                                </div>
                                <div class="text-right">
                                    <p class="font-bold <?= $transaction['type'] === 'earned' ? 'text-green-400' : ($transaction['type'] === 'spent' ? 'text-red-400' : 'text-blue-400') ?>">
                                        <?= $transaction['type'] === 'earned' ? '+' : ($transaction['type'] === 'spent' ? '-' : '+') ?><?= number_format($transaction['amount']) ?>
                                    </p>
                                    <?php if ($transaction['play_time'] > 0): ?>
                                        <p class="text-xs text-gray-500"><?= floor($transaction['play_time']) ?> min played</p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Withdrawal Requests -->
            <div class="bg-card-bg rounded-xl p-6">
                <h2 class="text-xl font-semibold mb-4">Withdrawal Requests</h2>
                
                <?php if (empty($withdrawals)): ?>
                    <div class="text-center py-8">
                        <i class="fas fa-money-bill-wave text-4xl text-gray-600 mb-4"></i>
                        <p class="text-gray-400">No withdrawal requests</p>
                        <p class="text-sm text-gray-500">Request a withdrawal when you're ready!</p>
                    </div>
                <?php else: ?>
                    <div class="space-y-3">
                        <?php foreach ($withdrawals as $withdrawal): ?>
                            <div class="p-3 bg-darker rounded-lg">
                                <div class="flex items-center justify-between mb-2">
                                    <span class="font-medium"><?= number_format($withdrawal['amount']) ?> coins</span>
                                    <span class="px-2 py-1 rounded text-xs font-medium
                                        <?= $withdrawal['status'] === 'pending' ? 'bg-yellow-600 text-yellow-100' : 
                                           ($withdrawal['status'] === 'approved' ? 'bg-green-600 text-green-100' : 'bg-red-600 text-red-100') ?>">
                                        <?= ucfirst($withdrawal['status']) ?>
                                    </span>
                                </div>
                                <div class="text-sm text-gray-400">
                                    <p>$<?= number_format($withdrawal['amount'] / ($settings['withdrawal_rate'] ?? 100), 2) ?> USD</p>
                                    <p><?= htmlspecialchars($withdrawal['payment_method']) ?></p>
                                    <p><?= date('M j, Y', strtotime($withdrawal['created_at'])) ?></p>
                                </div>
                                <?php if (!empty($withdrawal['admin_notes'])): ?>
                                    <div class="mt-2 p-2 bg-gray-800 rounded text-xs">
                                        <strong>Admin Notes:</strong> <?= htmlspecialchars($withdrawal['admin_notes']) ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../layout/footer.php'; ?>