<?php $config = require 'config_dispenser.php'; ?>
<!DOCTYPE html>
<html lang="pl">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IT Party</title>
    <link
        href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Rajdhani:wght@300;500;700&display=swap"
        rel="stylesheet">
    <style>
        :root {
            --stark-blue: #00a8ff;
            --stark-cyan: #00f3ff;
            --hud-bg: rgba(0, 10, 20, 0.85);
            --gold-accent: #ffd700;
        }

        body {
            background-color: #000;
            background-image: radial-gradient(circle at 50% 50%, #051a2e 0%, #000000 100%);
            color: var(--stark-cyan);
            font-family: 'Rajdhani', sans-serif;
            margin: 0;
            padding: 40px;
            overflow: hidden;
            height: 100vh;
            box-sizing: border-box;
        }

        h1 {
            font-family: 'Orbitron';
            color: var(--stark-blue);
            text-align: center;
            text-transform: uppercase;
            letter-spacing: 10px;
            font-size: 3rem;
            margin-bottom: 50px;
            text-shadow: 0 0 20px var(--stark-blue);
        }

        #leaderboard-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 30px;
            perspective: 1000px;
        }

        .guest-card {
            background: var(--hud-bg);
            border: 2px solid var(--stark-cyan);
            padding: 10px;
            position: relative;
            display: flex;
            flex-direction: column;
            align-items: center;
            transition: all 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            clip-path: polygon(20px 0, 100% 0, 100% calc(100% - 20px), calc(100% - 20px) 100%, 0 100%, 0 20px);
            box-shadow: 0 0 20px rgba(0, 243, 255, 0.2);
        }

        .guest-card.winner {
            border: 3px solid var(--gold-accent);
            box-shadow: 0 0 30px rgba(255, 215, 0, 0.4);
            transform: scale(1.05);
            z-index: 5;
        }

        .guest-card.winner .rank-badge {
            background: var(--gold-accent);
            color: #000;
            box-shadow: 0 0 15px var(--gold-accent);
        }

        .guest-photo {
            width: 100%;
            height: 200px;
            object-fit: cover;
            border: 1px solid var(--stark-blue);
            filter: grayscale(50%) brightness(1.2) contrast(1.1);
            margin-bottom: 10px;
        }

        .guest-name {
            font-family: 'Orbitron';
            font-size: 1.2rem;
            text-transform: uppercase;
            color: #fff;
            margin: 5px 0;
            text-shadow: 0 0 5px var(--stark-cyan);
        }

        .guest-points {
            font-family: 'Orbitron';
            font-size: 1.8rem;
            font-weight: 900;
            color: var(--gold-accent);
            text-shadow: 0 0 10px var(--gold-accent);
        }

        .rank-badge {
            position: absolute;
            top: 10px;
            left: 10px;
            background: var(--stark-cyan);
            color: #000;
            padding: 2px 10px;
            font-weight: bold;
            font-family: 'Orbitron';
            z-index: 10;
        }

        .alcohol-stats {
            margin-top: 15px;
            display: flex;
            gap: 20px;
            font-size: 1rem;
            color: var(--stark-cyan);
            text-transform: uppercase;
            border-top: 2px solid rgba(0, 243, 255, 0.3);
            padding-top: 15px;
            width: 100%;
            justify-content: center;
        }

        .alcohol-stat-item {
            text-align: center;
            line-height: 1.4;
        }

        .alcohol-val {
            display: block;
            font-weight: 900;
            font-size: 1.2rem;
            color: #fff;
            text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
        }
    </style>
</head>

<body>
    <h1>IT PARTY DASHBOARD</h1>
    <div id="leaderboard-grid"></div>

    <script>
        let previousData = [];
        const drinkConfig = <?php echo json_encode($config['drinks']); ?>;

        async function updateLeaderboard() {
            try {
                const response = await fetch('get_leaderboard.php');
                const data = await response.json();

                if (data.status === 'success') {
                    if (data.leaderboard.length === 0) {
                        document.getElementById('leaderboard-grid').innerHTML = '<div style="grid-column: 1/-1; text-align: center; font-size: 2rem; opacity: 0.5; margin-top: 100px;">OCZEKIWANIE NA IDENTYFIKACJĘ...</div>';
                        previousData = [];
                    } else {
                        renderLeaderboard(data.leaderboard);
                    }
                }
            } catch (e) {
                console.error("Błąd aktualizacji rankingu", e);
            }
        }

        function renderLeaderboard(leaderboard) {
            const grid = document.getElementById('leaderboard-grid');

            const currentDataStr = JSON.stringify(leaderboard);
            if (currentDataStr === JSON.stringify(previousData)) return;

            grid.innerHTML = '';
            leaderboard.forEach((guest, index) => {
                const card = document.createElement('div');
                card.className = 'guest-card' + (index === 0 ? ' winner' : '');
                card.style.animation = `fadeSlideIn 0.5s ease forwards ${index * 0.1}s`;

                const rank = index + 1;
                const photo = guest.photo_path || 'assets/placeholder.png';

                // Generate alcohol stats dynamically
                let alcoholHtml = '';
                for (const key in drinkConfig) {
                    const drink = drinkConfig[key];
                    const val = guest[drink.db_column] || 0;
                    alcoholHtml += `<div class="alcohol-stat-item">${drink.label.toUpperCase()} <span class="alcohol-val">${val}ml</span></div>`;
                }

                card.innerHTML = `
                    <div class="rank-badge">#${rank}</div>
                    <img src="${photo}" class="guest-photo" alt="${guest.name}">
                    <div class="guest-name">${guest.name}</div>
                    <div class="guest-points">${guest.points} PKT</div>
                    <div class="alcohol-stats">
                        ${alcoholHtml}
                    </div>
                `;
                grid.appendChild(card);
            });

            previousData = leaderboard;
        }

        // CSS for entry animation
        const style = document.createElement('style');
        style.innerHTML = `
            @keyframes fadeSlideIn {
                from { opacity: 0; transform: translateY(20px) scale(0.9); }
                to { opacity: 1; transform: translateY(0) scale(1); }
            }

            .winner {
                animation: fadeSlideIn 0.5s ease forwards, pulseWinner 2s infinite ease-in-out !important;
            }

            @keyframes pulseWinner {
                0% { box-shadow: 0 0 20px rgba(255, 215, 0, 0.3); }
                50% { box-shadow: 0 0 40px rgba(255, 215, 0, 0.6); }
                100% { box-shadow: 0 0 20px rgba(255, 215, 0, 0.3); }
            }
        `;
        document.head.appendChild(style);

        // Initial load and interval
        updateLeaderboard();
        setInterval(updateLeaderboard, 5000); // Poll every 5 seconds
    </script>
</body>

</html>