<?php
$pageTitle = 'ভিডিও';
require_once 'header.php';
$db = getDB();

$page = max(1, intval($_GET['page'] ?? 1));
$offset = ($page - 1) * ITEMS_PER_PAGE;
$total = $db->query("SELECT COUNT(*) FROM videos")->fetchColumn();
$totalPages = ceil($total / ITEMS_PER_PAGE);

$vids = $db->prepare("SELECT v.*, c.boy_name, c.girl_name FROM videos v LEFT JOIN couples c ON v.couple_id=c.id ORDER BY v.created_at DESC LIMIT ? OFFSET ?");
$vids->bindValue(1, ITEMS_PER_PAGE, PDO::PARAM_INT);
$vids->bindValue(2, $offset, PDO::PARAM_INT);
$vids->execute();
$vids = $vids->fetchAll();

// Also get couple videos
$coupleVids = $db->query("SELECT id, boy_name, girl_name, press_video_link, video_upload FROM couples WHERE status='approved' AND (press_video_link IS NOT NULL AND press_video_link != '' OR video_upload IS NOT NULL AND video_upload != '') ORDER BY created_at DESC LIMIT 12")->fetchAll();
?>

<section class="section" style="padding-top:120px;">
  <div class="container">
    <h2 class="section-title">🎬 ভিডিও সেকশন</h2>
    <p style="text-align:center;color:var(--text-muted);margin-bottom:40px;">প্রেস কনফারেন্স, ভাইরাল ক্লিপ এবং আরও অনেক কিছু</p>

    <?php if ($vids || $coupleVids): ?>
    <div class="card-grid">
      <?php foreach ($vids as $v): ?>
      <div class="video-card reveal">
        <div class="video-wrapper">
          <?php $ytId = getYouTubeId($v['video_url']); ?>
          <?php if ($ytId): ?>
            <iframe src="https://www.youtube.com/embed/<?= $ytId ?>" allowfullscreen loading="lazy"></iframe>
          <?php else: ?>
            <video controls preload="metadata"><source src="<?= UPLOAD_URL . e($v['video_url']) ?>" type="video/mp4"></video>
          <?php endif; ?>
        </div>
        <div class="video-info">
          <p class="video-title"><?= e($v['title']) ?></p>
          <?php if ($v['boy_name']): ?>
          <p style="color:var(--text-muted);font-size:.85rem;"><?= e($v['boy_name']) ?> ❤ <?= e($v['girl_name']) ?></p>
          <?php endif; ?>
          <?php if ($v['description']): ?>
          <p style="color:var(--text-muted);font-size:.85rem;margin-top:4px;"><?= e(truncate($v['description'], 100)) ?></p>
          <?php endif; ?>
        </div>
      </div>
      <?php endforeach; ?>

      <?php foreach ($coupleVids as $cv): ?>
        <?php $ytId = getYouTubeId($cv['press_video_link']); ?>
        <?php if ($ytId): ?>
        <div class="video-card reveal">
          <div class="video-wrapper">
            <iframe src="https://www.youtube.com/embed/<?= $ytId ?>" allowfullscreen loading="lazy"></iframe>
          </div>
          <div class="video-info">
            <p class="video-title"><?= e($cv['boy_name']) ?> ❤ <?= e($cv['girl_name']) ?> — প্রেস কনফারেন্স</p>
          </div>
        </div>
        <?php endif; ?>
        <?php if ($cv['video_upload']): ?>
        <div class="video-card reveal">
          <div class="video-wrapper">
            <video controls preload="metadata"><source src="<?= UPLOAD_URL . e($cv['video_upload']) ?>" type="video/mp4"></video>
          </div>
          <div class="video-info">
            <p class="video-title"><?= e($cv['boy_name']) ?> ❤ <?= e($cv['girl_name']) ?></p>
          </div>
        </div>
        <?php endif; ?>
      <?php endforeach; ?>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="pagination">
      <?php if ($page > 1): ?><a href="?page=<?= $page-1 ?>">« আগে</a><?php endif; ?>
      <?php for ($i = 1; $i <= $totalPages; $i++): ?>
        <?php if ($i == $page): ?><span class="active"><?= bnNum($i) ?></span>
        <?php else: ?><a href="?page=<?= $i ?>"><?= bnNum($i) ?></a><?php endif; ?>
      <?php endfor; ?>
      <?php if ($page < $totalPages): ?><a href="?page=<?= $page+1 ?>">পরে »</a><?php endif; ?>
    </div>
    <?php endif; ?>

    <?php else: ?>
    <div class="glass" style="text-align:center;padding:48px;">
      <p style="font-size:3rem;">🎬</p>
      <p style="color:var(--text-muted);">এখনো কোনো ভিডিও নেই।</p>
    </div>
    <?php endif; ?>
  </div>
</section>

<?php require_once 'footer.php'; ?>
