<?php
// ==========================================
// 1. CONFIGURATION & SEO SETUP
// ==========================================
require_once '../config.php';
require '../components/db_connect.php';

// SEO VARIABLES
$page_title = "Swarnawahini Events | Watch Latest Highlights";
$page_description = "Watch the latest events and exclusive YouTube shorts from Swarnawahini with our special Kokis theme.";
$page_url = "https://www.swarnavahini.lk/pages/events.php";

// PAGINATION LOGIC
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$items_per_page = 8; 
$offset = ($page - 1) * $items_per_page;
$shorts = [];
$total_pages = 1;

try {
    $count_stmt = $conn->query("SELECT COUNT(*) FROM youtube_shorts_events");
    $total_items = $count_stmt->fetchColumn();
    $total_pages = ceil($total_items / $items_per_page);
    
    $stmt = $conn->prepare("SELECT * FROM youtube_shorts_events ORDER BY published_at DESC LIMIT :limit OFFSET :offset");
    $stmt->bindValue(':limit', $items_per_page, PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    $shorts = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    error_log("DB Error: " . $e->getMessage());
}

require '../components/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?></title>
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">

    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background: rgb(25, 52, 134);
            color: white;
            padding-top: 90px;
            overflow-x: hidden;
        }

        /* --- KOKIS RAIN CSS --- */
        #kokis-rain-container {
            position: fixed;
            top: 0; left: 0; width: 100%; height: 100%;
            pointer-events: none; z-index: 9999; overflow: hidden;
        }

        .kokis-item {
            position: absolute; top: -100px; width: 40px; opacity: 0.7;
            filter: drop-shadow(0 0 5px rgba(0,0,0,0.3));
            animation: fall linear infinite;
        }

        @keyframes fall {
            0% { transform: translateY(0) rotate(0deg); }
            100% { transform: translateY(110vh) rotate(360deg); }
        }

        /* --- LAYOUT STYLES --- */
        .glass-container {
            max-width: 1200px; margin: 40px auto; padding: 20px;
            background: rgba(255, 255, 255, 0.1); backdrop-filter: blur(5px);
            border-radius: 15px; border: 1px solid rgba(255, 255, 255, 0.2);
            width: 95%; position: relative; z-index: 10;
        }

        .page-title {
            text-align: left; font-weight: 600; margin: 30px 0;
            color: #FFC107; font-size: 2.5rem;
        }

        .shorts-card {
            background: rgba(0, 0, 0, 0.3); border: 1px solid rgba(255, 255, 255, 0.15);
            border-radius: 20px; overflow: hidden; position: relative;
            width: 100%; aspect-ratio: 9 / 16;
            transition: transform 0.3s ease, border-color 0.3s ease;
        }

        .shorts-card:hover { transform: translateY(-8px); border-color: #FFC107; }
        .shorts-card iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none; }

        /* --- SYNCHRONIZED PAGINATION STYLES --- */
        .pagination {
            margin-top: 2rem;
            display: flex;
            justify-content: center;
            gap: 0.5rem;
            flex-wrap: wrap;
        }

        .pagination .page-item {
            display: inline-flex;
        }

        .pagination .page-link {
            color: #1A237E;
            background-color: rgba(255, 255, 255, 0.8);
            border: 1px solid rgba(0, 25, 118, 0.2);
            padding: 8px 15px;
            min-width: 40px;
            text-align: center;
            border-radius: 50px; /* Matching the pill shape */
            transition: background-color 0.3s ease, transform 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 16px;
        }

        .pagination .page-item.active .page-link {
            background-color: #FFC107;
            color: #1A237E;
            border-color: #FFC107;
            transform: scale(1.1);
        }

        .pagination .page-item.disabled .page-link {
            background-color: rgba(255, 255, 255, 0.5);
            color: #6c757d;
            opacity: 0.7;
            cursor: not-allowed;
        }

        .pagination .page-link:hover:not(.disabled) {
            background-color: #ff0000;
            color: #ffffff;
            transform: scale(1.05);
        }

        /* Responsive Breakpoints from Teledrama Page */
        @media (max-width: 991px) {
            .pagination { gap: 0.4rem; }
            .pagination .page-link { padding: 6px 12px; min-width: 36px; font-size: 14px; }
        }

        @media (max-width: 576px) {
            .pagination { gap: 0.3rem; padding: 0 10px; }
            .pagination .page-link { padding: 5px 10px; min-width: 32px; font-size: 12px; }
            .pagination .page-item { margin: 2px 0; }
        }

        @media (max-width: 400px) {
            .pagination { gap: 0.2rem; }
            .pagination .page-link { padding: 4px 8px; min-width: 28px; font-size: 11px; }
        }
    </style>
</head>
<body>

<div id="kokis-rain-container"></div>

<div class="container-fluid">
    <div class="glass-container">
        
        <h2 class="page-title">Awurudu 2026</h2>
        
        <div class="row g-4 justify-content-center">
            <?php if (!empty($shorts)): ?>
                <?php foreach ($shorts as $short): ?>
                    <div class="col-12 col-sm-6 col-lg-3"> 
                        <div class="shorts-card">
                            <iframe 
                                src="https://www.youtube.com/embed/<?php echo $short['video_id']; ?>?rel=0&modestbranding=1&playsinline=1" 
                                title="<?php echo htmlspecialchars($short['title']); ?>" 
                                allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" 
                                allowfullscreen>
                            </iframe>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="col-12 text-center py-5">
                    <p class="text-white-50">No highlights available yet.</p>
                </div>
            <?php endif; ?>
        </div>

        <?php if ($total_pages > 1): ?>
            <nav aria-label="Events pagination" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
                        <a class="page-link" href="?page=<?php echo $page - 1; ?>" aria-label="Previous">
                            <span aria-hidden="true">«</span>
                        </a>
                    </li>

                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
                            <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                        </li>
                    <?php endfor; ?>

                    <li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
                        <a class="page-link" href="?page=<?php echo $page + 1; ?>" aria-label="Next">
                            <span aria-hidden="true">»</span>
                        </a>
                    </li>
                </ul>
            </nav>
        <?php endif; ?>

    </div>
</div>

<script>
    function startKokisRain() {
        const container = document.getElementById('kokis-rain-container');
        const count = 15; 
        for (let i = 0; i < count; i++) { createKokis(container); }
    }

    function createKokis(container) {
        const kokis = document.createElement('img');
        kokis.src = '../uploads/kokis.png'; 
        kokis.className = 'kokis-item';
        const left = Math.random() * 100;
        const duration = 7 + Math.random() * 8; 
        const delay = Math.random() * 5;
        const size = 15 + Math.random() * 20; 
        kokis.style.left = left + 'vw';
        kokis.style.animationDuration = duration + 's';
        kokis.style.animationDelay = delay + 's';
        kokis.style.width = size + 'px';
        container.appendChild(kokis);
        kokis.addEventListener('animationiteration', () => {
            kokis.style.left = Math.random() * 100 + 'vw';
        });
    }
    document.addEventListener('DOMContentLoaded', startKokisRain);
</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>

<?php require '../components/footer.php'; ?>