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

// YOUR VIDEO LINK (Update this URL for the specific event)
// $stream_url = "https://iframes.5centscdn.com/your-new-stream-link-here?showcv=true&autoplay=true&muted=true";

// YOUR THUMBNAIL 
$thumbnail_url = "https://www.swarnavahini.lk/Uploads/tharureyak.jpg";

// SEO VARIABLES - Updated for Tharu Reyak 2026
$page_title = "Watch Tharu Reyak 2026 | Full Event | Swarnawahini";
$page_description = "Watch the exclusive Tharu Reyak 2026 full event and highlights on Swarnawahini. Experience the biggest stars and musical performances in high definition.";
$page_keywords = "Tharu Reyak 2026, Tharu Reyak Full Video, Swarnawahini Live, Sinhala Musical Show 2026, Sri Lankan Entertainment";
$page_url = "https://www.swarnavahini.lk/pages/tharureyak.php";

// ==========================================
// PAGINATION LOGIC (Updated for youtube_shorts_new)
// ==========================================
$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 {
    // Querying the NEW table for this specific page
    $count_stmt = $conn->query("SELECT COUNT(*) FROM youtube_shorts_new");
    $total_items = $count_stmt->fetchColumn();
    $total_pages = ceil($total_items / $items_per_page);

    $stmt = $conn->prepare("SELECT * FROM youtube_shorts_new 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>
    <meta name="description" content="<?php echo $page_description; ?>">
    <meta name="keywords" content="<?php echo $page_keywords; ?>">
    <link rel="canonical" href="<?php echo $page_url; ?>">
    <meta name="robots" content="index, follow">

    <meta property="og:type" content="video.movie">
    <meta property="og:title" content="<?php echo $page_title; ?>">
    <meta property="og:description" content="<?php echo $page_description; ?>">
    <meta property="og:url" content="<?php echo $page_url; ?>">
    <meta property="og:image" content="<?php echo $thumbnail_url; ?>">
    <meta property="og:site_name" content="Swarnavahini">

    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="<?php echo $page_title; ?>">
    <meta name="twitter:description" content="<?php echo $page_description; ?>">
    <meta name="twitter:image" content="<?php echo $thumbnail_url; ?>">

    <script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "VideoObject",
      "name": "Tharu Reyak 2026 Full Event",
      "description": "<?php echo $page_description; ?>",
      "thumbnailUrl": ["<?php echo $thumbnail_url; ?>"],
      "uploadDate": "2026-04-12T20:00:00+05:30",
      "contentUrl": "<?php echo $stream_url; ?>",
      "embedUrl": "<?php echo $page_url; ?>",
      "publisher": { 
          "@type": "Organization", 
          "name": "Swarnavahini", 
          "logo": { "@type": "ImageObject", "url": "https://www.swarnavahini.lk/assets/images/logo.png" } 
      }
    }
    </script>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
    
    <style>
        /* --- GLOBAL STYLES --- */
        body {
            font-family: 'Poppins', sans-serif;
            background: rgb(25, 52, 134);
            color: white;
            min-height: 100vh;
            padding-top: 90px; 
            padding-bottom: 80px;
            overflow-x: hidden;
        }

        /* --- MAIN GLASS CONTAINER --- */
        .glass-container {
            max-width: 1200px;
            margin: 40px auto;
            padding: 20px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            border-radius: 15px;
            box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
            border: 1px solid rgba(255, 255, 255, 0.18);
            width: 95%; 
            position: relative;
            z-index: 1; 
        }

        .page-title {
            text-align: center;
            font-weight: 600;
            margin-bottom: 30px;
            color: #FFC107;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
            font-size: 2.2rem;
        }

        /* --- VIDEO CONTAINER --- */
        .live-placeholder-wrapper {
            background-color: #000;
            background-image: url('<?php echo $thumbnail_url; ?>');
            background-size: cover;
            background-position: center;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 0 25px rgba(255, 193, 7, 0.3);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        #live-iframe { 
            width: 100%; 
            height: 100%; 
            opacity: 0; 
            transition: opacity 1.5s ease-in-out; 
        }
        
        #live-iframe.visible { opacity: 1; }

        .seo-content { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

        /* --- SHORTS CARD --- */
        .shorts-card {
            background: rgba(255, 255, 255, 0.08);
            backdrop-filter: blur(20px);
            border: 1px solid rgba(255, 255, 255, 0.15);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            border-radius: 20px;
            overflow: hidden;
            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;
        }

        /* --- PAGINATION --- */
        .pagination { margin-top: 3rem; display: flex; justify-content: center; gap: 0.5rem; }
        .pagination .page-link {
            color: #1A237E; background-color: rgba(255, 255, 255, 0.9);
            border: none; padding: 10px 18px; border-radius: 50px;
            transition: all 0.3s ease; font-weight: 600;
        }
        .pagination .page-item.active .page-link { background-color: #FFC107; color: #1A237E; transform: scale(1.1); }
        .pagination .page-link:hover { background-color: #ff0000; color: #ffffff; }

        @media (max-width: 991px) {
            .page-title { font-size: 1.8rem; }
        }
    </style>
</head>
<body>

<div class="seo-content">
    <h1>Watch Tharu Reyak 2026 Full Event Highlights</h1>
    <p><?php echo $page_description; ?></p>
</div>

<div class="container-fluid p-0">
    <div class="glass-container">
        
        <div class="row mb-5 justify-content-center">
            <div class="col-12 col-lg-10"> 
                <div class="live-placeholder-wrapper">
                    <div class="ratio ratio-16x9">
                        <div id="live-video-box">
                           <div style="width:100%;height:100%">
  <iframe src="https://iframes.5centscdn.com/5centscdn/auto/skin1/0yu9oyimz3zhgmqw/aHR0cHM6Ly9tMnFhbDY1eWFqZGctaGxzLXB1c2guNWNlbnRzY2RuLmNvbS9tcDQvVGhhcnUlMjBSYXlhayUyMDIwMjYlMjAtJTIwTG93Lm1wNC9wbGF5bGlzdC5tM3U4?showcv=true" frameBorder="0" allow="autoplay; encrypted-media" allowfullscreen
    style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0;left:0;right:0;bottom:0">
  </iframe>
</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <h2 class="page-title">Event Highlights</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">New highlights coming soon. Stay tuned!</p>
                </div>
            <?php endif; ?>
        </div>

        <?php if ($total_pages > 1): ?>
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li class="page-item <?php echo ($page <= 1) ? 'disabled' : ''; ?>">
                        <a class="page-link" href="?page=<?php echo $page - 1; ?>"><i class="bi bi-chevron-left"></i></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; ?>"><i class="bi bi-chevron-right"></i></a>
                    </li>
                </ul>
            </nav>
        <?php endif; ?>

    </div>
</div>

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

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const iframe = document.getElementById('live-iframe');
        
        iframe.onload = function() {
            setTimeout(function() {
                iframe.classList.add('visible');
            }, 500); 
        };

        // Fallback in case onload event doesn't fire
        setTimeout(function() {
            if(!iframe.classList.contains('visible')) {
                iframe.classList.add('visible');
            }
        }, 4000); 
    });
</script>

</body>
</html>

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