<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Points History - LocalLendables</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .points-summary {
            background: linear-gradient(135deg, #007bff, #6610f2);
            color: white;
            padding: 2rem;
            border-radius: 15px;
            margin-bottom: 2rem;
        }
        
        .transaction-item {
            border-left: 4px solid #dee2e6;
            margin-bottom: 1rem;
            transition: all 0.3s ease;
        }
        
        .transaction-item:hover {
            background: #f8f9fa;
            border-left-color: #007bff;
        }
        
        .transaction-positive {
            border-left-color: #28a745 !important;
        }
        
        .transaction-negative {
            border-left-color: #dc3545 !important;
        }
        
        .points-badge {
            padding: 0.5rem 1rem;
            border-radius: 25px;
            font-weight: bold;
            font-size: 0.9rem;
        }
        
        .points-positive {
            background: linear-gradient(135deg, #28a745, #20c997);
            color: white;
        }
        
        .points-negative {
            background: linear-gradient(135deg, #dc3545, #fd7e14);
            color: white;
        }
        
        .redemption-card {
            border: 1px solid #dee2e6;
            border-radius: 10px;
            margin-bottom: 1rem;
            transition: transform 0.2s ease;
        }
        
        .redemption-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }
        
        .status-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 12px;
            font-size: 0.8rem;
            font-weight: 500;
        }
        
        .status-pending {
            background: #fff3cd;
            color: #856404;
        }
        
        .status-fulfilled {
            background: #d1edff;
            color: #0c5460;
        }
    </style>
</head>
<body class="bg-light">
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-success">
        <div class="container">
            <a class="navbar-brand fw-bold" href="/dashboard/{{ currentUser.id }}">
                <i class="fas fa-leaf me-2"></i>LocalLendables
            </a>
            <div class="d-flex align-items-center text-white">
                <a href="/rewards" class="btn btn-outline-light btn-sm me-2">
                    <i class="fas fa-star me-1"></i>Rewards
                </a>
                <a href="/dashboard/{{ currentUser.id }}" class="btn btn-outline-light btn-sm">
                    <i class="fas fa-home me-1"></i>Dashboard
                </a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <!-- Points Summary -->
        <div class="points-summary">
            <div class="row align-items-center">
                <div class="col-md-6">
                    <h2 class="mb-1">
                        <i class="fas fa-chart-line me-2"></i>Points Summary
                    </h2>
                    <div class="row">
                        <div class="col-6">
                            <h4 class="mb-0">{{ userPoints.totalPoints }}</h4>
                            <small class="opacity-75">Current Balance</small>
                        </div>
                        <div class="col-6">
                            <h4 class="mb-0">{{ userPoints.lifetimePoints }}</h4>
                            <small class="opacity-75">Lifetime Earned</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-6 text-end">
                    <div class="opacity-75">
                        <i class="fas fa-trophy fa-3x"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Points Transactions -->
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-exchange-alt me-2"></i>Recent Transactions
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if transactions|length > 0 %}
                            {% for transaction in transactions %}
                            <div class="transaction-item {{ transaction.points_change > 0 ? 'transaction-positive' : 'transaction-negative' }} p-3">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div class="flex-grow-1">
                                        <h6 class="mb-1">{{ transaction.description }}</h6>
                                        <small class="text-muted">
                                            <i class="fas fa-clock me-1"></i>
                                            {{ transaction.created_at|date('M j, Y g:i A') }}
                                        </small>
                                        {% if transaction.transaction_type %}
                                            <span class="badge bg-secondary ms-2">{{ transaction.transaction_type }}</span>
                                        {% endif %}
                                    </div>
                                    <div class="points-badge {{ transaction.points_change > 0 ? 'points-positive' : 'points-negative' }}">
                                        {{ transaction.points_change > 0 ? '+' : '' }}{{ transaction.points_change }}
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        {% else %}
                            <div class="text-center py-4">
                                <i class="fas fa-history fa-3x text-muted mb-3"></i>
                                <h5 class="text-muted">No transactions yet</h5>
                                <p class="text-muted">Start lending and borrowing to earn points!</p>
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- Redemptions -->
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-gift me-2"></i>Redemptions
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if redemptions|length > 0 %}
                            {% for redemption in redemptions %}
                            <div class="redemption-card p-3">
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <h6 class="mb-0">{{ redemption.reward_name }}</h6>
                                    <span class="status-badge {{ redemption.status == 'fulfilled' ? 'status-fulfilled' : 'status-pending' }}">
                                        {{ redemption.status|title }}
                                    </span>
                                </div>
                                {% if redemption.sponsor_name %}
                                    <small class="text-muted d-block mb-1">
                                        <i class="fas fa-handshake me-1"></i>{{ redemption.sponsor_name }}
                                    </small>
                                {% endif %}
                                <div class="d-flex justify-content-between align-items-center">
                                    <small class="text-muted">
                                        {{ redemption.redeemed_at|date('M j, Y') }}
                                    </small>
                                    <span class="badge bg-primary">{{ redemption.points_spent }} pts</span>
                                </div>
                                {% if redemption.notes and 'Coupon Code:' in redemption.notes %}
                                    <div class="mt-2">
                                        <button class="btn btn-sm btn-outline-primary" onclick="showCoupon('{{ redemption.notes|replace('Coupon Code: ', '') }}', '{{ redemption.reward_name }}')">
                                            <i class="fas fa-qrcode me-1"></i>View Coupon
                                        </button>
                                    </div>
                                {% endif %}
                            </div>
                            {% endfor %}
                        {% else %}
                            <div class="text-center py-4">
                                <i class="fas fa-gift fa-2x text-muted mb-2"></i>
                                <p class="text-muted mb-0">No redemptions yet</p>
                                <a href="/rewards" class="btn btn-sm btn-primary mt-2">Browse Rewards</a>
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Coupon Modal -->
    <div class="modal fade" id="couponModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-qrcode me-2"></i>Your Coupon
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body text-center" id="couponContent">
                    <!-- Content populated by JavaScript -->
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="printCoupon()">
                        <i class="fas fa-print me-1"></i>Print
                    </button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/qrcode@1.5.3/build/qrcode.min.js"></script>
    <script>
        let currentCouponCode = '';
        let currentRewardName = '';

        async function showCoupon(couponCode, rewardName) {
            currentCouponCode = couponCode;
            currentRewardName = rewardName;
            
            const content = document.getElementById('couponContent');
            
            // Generate QR code
            const qrCanvas = document.createElement('canvas');
            await QRCode.toCanvas(qrCanvas, couponCode, {
                width: 200,
                margin: 2,
                color: {
                    dark: '#000000',
                    light: '#FFFFFF'
                }
            });
            
            content.innerHTML = `
                <h5 class="mb-3">${rewardName}</h5>
                <div class="mb-3">
                    ${qrCanvas.outerHTML}
                </div>
                <div class="coupon-code p-3 bg-light rounded">
                    <strong>Coupon Code:</strong><br>
                    <span class="h6 font-monospace text-primary">${couponCode}</span>
                </div>
                <small class="text-muted mt-2 d-block">
                    Present this QR code or coupon code to redeem your reward
                </small>
            `;
            
            new bootstrap.Modal(document.getElementById('couponModal')).show();
        }

        function printCoupon() {
            const printWindow = window.open('', '_blank');
            const couponContent = document.getElementById('couponContent').innerHTML;
            
            printWindow.document.write(`
                <html>
                <head>
                    <title>LocalLendables Coupon - ${currentRewardName}</title>
                    <style>
                        body { font-family: Arial, sans-serif; margin: 20px; text-align: center; }
                        .coupon-container { border: 2px solid #007bff; padding: 20px; }
                        .text-primary { color: #007bff; }
                        .font-monospace { font-family: monospace; }
                        .bg-light { background-color: #f8f9fa; }
                        .rounded { border-radius: 0.375rem; }
                    </style>
                </head>
                <body>
                    <div class="coupon-container">
                        <h2 style="color: #28a745;">LocalLendables Reward Coupon</h2>
                        ${couponContent}
                    </div>
                </body>
                </html>
            `);
            
            printWindow.document.close();
            printWindow.print();
        }
    </script>
</body>
</html>