<?php
require_once 'config/config.php';
$db = getDB();
$id = intval($_GET['id'] ?? 0);
if (!$id) { header('Location: index.php'); exit; }

// View counter AJAX
if (isset($_GET['view'])) {
    $db->prepare("UPDATE couples SET views = views + 1 WHERE id = ? AND status='approved'")->execute([$id]);
    echo json_encode(['ok'=>true]); exit;
}

// Comment submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comment'])) {
    if (verifyCSRFToken($_POST['csrf_token'] ?? '')) {
        $name = trim($_POST['commenter_name'] ?? '');
        $text = trim($_POST['comment_text'] ?? '');
        if ($name && $text) {
            $db->prepare("INSERT INTO comments (couple_id, commenter_name, comment_text) VALUES (?,?,?)")->execute([$id, $name, $text]);
        }
    }
    header("Location: couple.php?id=$id#comments"); exit;
}

$couple = $db->prepare("SELECT * FROM couples WHERE id = ? AND status='approved'");
$couple->execute([$id]);
$couple = $couple->fetch();
if (!$couple) { header('Location: index.php'); exit; }

$pageTitle = e($couple['boy_name']) . ' ❤ ' . e($couple['girl_name']);
$pageDesc = $couple['short_story'] ? truncate($couple['short_story'], 160) : 'তজিয়া গ্রুপে এই কাপলের গল্প পড়ুন';
$pageUrl = SITE_URL . '/couple.php?id=' . $id;

// Gallery
$gallery = $db->prepare("SELECT * FROM galleries WHERE couple_id = ?");
$gallery->execute([$id]);
$gallery = $gallery->fetchAll();

// Videos
$videos = $db->prepare("SELECT * FROM videos WHERE couple_id = ?");
$videos->execute([$id]);
$videos = $videos->fetchAll();

// Comments
$comments = $db->prepare("SELECT * FROM comments WHERE couple_id = ? AND status='approved' ORDER BY created_at DESC");
$comments->execute([$id]);
$comments = $comments->fetchAll();

$csrf = generateCSRFToken();
require_once 'header.php';
?>

<!-- Couple Hero -->
<div class="couple-hero" style="background-image:url('<?= $couple['photo'] ? UPLOAD_URL . e($couple['photo']) : '' ?>');" data-couple-id="<?= $id ?>">
  <?php if (!$couple['photo']): ?>
  <div style="position:absolute;inset:0;background:var(--gradient);opacity:0.3;"></div>
  <?php endif; ?>
  <div class="couple-hero-content">
    <h1><?= e($couple['boy_name']) ?> ❤ <?= e($couple['girl_name']) ?></h1>
    <?php if ($couple['funny_title']): ?>
    <span class="card-badge" style="font-size:1rem;margin-top:8px;"><?= e($couple['funny_title']) ?></span>
    <?php endif; ?>
    <?php if ($couple['status_text']): ?>
    <p style="color:#ddd;margin-top:8px;font-style:italic;">"<?= e($couple['status_text']) ?>"</p>
    <?php endif; ?>
  </div>
</div>

<section class="section">
  <div class="container" style="max-width:900px;">

    <!-- Info Cards -->
    <div class="info-grid reveal">
      <?php if ($couple['nickname']): ?>
      <div class="info-item"><i class="fas fa-heart"></i><div><small style="color:var(--text-muted);">ডাকনাম</small><br><?= e($couple['nickname']) ?></div></div>
      <?php endif; ?>
      <?php if ($couple['marriage_date']): ?>
      <div class="info-item"><i class="fas fa-calendar-alt"></i><div><small style="color:var(--text-muted);">বিয়ের তারিখ</small><br><?= bnDate($couple['marriage_date']) ?></div></div>
      <?php endif; ?>
      <?php if ($couple['district']): ?>
      <div class="info-item"><i class="fas fa-map-marker-alt"></i><div><small style="color:var(--text-muted);">অবস্থান</small><br><?= e($couple['district']) ?><?= $couple['upazila'] ? ', '.e($couple['upazila']) : '' ?></div></div>
      <?php endif; ?>
      <?php if ($couple['love_duration']): ?>
      <div class="info-item"><i class="fas fa-clock"></i><div><small style="color:var(--text-muted);">ভালোবাসার সময়কাল</small><br><?= e($couple['love_duration']) ?></div></div>
      <?php endif; ?>
      <div class="info-item"><i class="fas fa-eye"></i><div><small style="color:var(--text-muted);">মোট ভিউ</small><br><?= bnNum($couple['views']) ?></div></div>
      <div class="info-item"><i class="fas fa-clock"></i><div><small style="color:var(--text-muted);">যোগ হয়েছে</small><br><?= bnTimeAgo($couple['created_at']) ?></div></div>
    </div>

    <!-- Story -->
    <?php if ($couple['short_story']): ?>
    <div class="glass reveal" style="margin-bottom:32px;">
      <h3 style="margin-bottom:12px;"><i class="fas fa-book-open"></i> তাদের গল্প</h3>
      <p style="line-height:1.8;color:var(--text-muted);"><?= nl2br(e($couple['short_story'])) ?></p>
    </div>
    <?php endif; ?>

    <!-- Social Links & Share -->
    <div class="glass reveal" style="margin-bottom:32px;">
      <h3 style="margin-bottom:12px;"><i class="fas fa-share-alt"></i> শেয়ার করুন</h3>
      <div class="share-btns">
        <a href="https://www.facebook.com/sharer.php?u=<?= urlencode($pageUrl) ?>" target="_blank" class="share-btn fb"><i class="fab fa-facebook-f"></i></a>
        <a href="https://twitter.com/intent/tweet?url=<?= urlencode($pageUrl) ?>&text=<?= urlencode($couple['boy_name'].' ❤ '.$couple['girl_name']) ?>" target="_blank" class="share-btn tw"><i class="fab fa-twitter"></i></a>
        <a href="https://api.whatsapp.com/send?text=<?= urlencode($couple['boy_name'].' ❤ '.$couple['girl_name'].' - '.$pageUrl) ?>" target="_blank" class="share-btn wa"><i class="fab fa-whatsapp"></i></a>
        <?php if ($couple['facebook_link']): ?>
        <a href="<?= e($couple['facebook_link']) ?>" target="_blank" class="btn btn-sm btn-outline"><i class="fab fa-facebook"></i> ফেসবুক প্রোফাইল</a>
        <?php endif; ?>
      </div>
    </div>

    <!-- Gallery -->
    <?php if ($gallery): ?>
    <div class="reveal" style="margin-bottom:32px;">
      <h3 style="margin-bottom:16px;"><i class="fas fa-images"></i> ফটো গ্যালারি</h3>
      <div class="masonry">
        <?php foreach ($gallery as $img): ?>
        <div class="masonry-item" data-lightbox="<?= UPLOAD_URL . e($img['image_path']) ?>">
          <img src="<?= UPLOAD_URL . e($img['image_path']) ?>" alt="Gallery">
          <div class="masonry-overlay"><i class="fas fa-search-plus" style="color:#fff;font-size:1.5rem;"></i></div>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
    <?php endif; ?>

    <!-- Videos -->
    <?php
    $ytId = getYouTubeId($couple['press_video_link']);
    if ($ytId || $couple['video_upload'] || $videos):
    ?>
    <div class="reveal" style="margin-bottom:32px;">
      <h3 style="margin-bottom:16px;"><i class="fas fa-video"></i> ভিডিও</h3>
      <?php if ($ytId): ?>
      <div class="video-card" style="margin-bottom:16px;">
        <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">প্রেস কনফারেন্স ভিডিও</p></div>
      </div>
      <?php endif; ?>
      <?php if ($couple['video_upload']): ?>
      <div class="video-card" style="margin-bottom:16px;">
        <div class="video-wrapper">
          <video controls preload="metadata"><source src="<?= UPLOAD_URL . e($couple['video_upload']) ?>" type="video/mp4"></video>
        </div>
        <div class="video-info"><p class="video-title">আপলোডকৃত ভিডিও</p></div>
      </div>
      <?php endif; ?>
      <?php foreach ($videos as $v): ?>
      <div class="video-card" style="margin-bottom:16px;">
        <div class="video-wrapper">
          <?php $vid = getYouTubeId($v['video_url']); ?>
          <?php if ($vid): ?>
            <iframe src="https://www.youtube.com/embed/<?= $vid ?>" 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></div>
      </div>
      <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- Comments -->
    <div class="reveal" id="comments">
      <h3 style="margin-bottom:16px;"><i class="fas fa-comments"></i> মন্তব্য (<?= bnNum(count($comments)) ?>)</h3>
      <form method="POST" class="glass" style="margin-bottom:24px;">
        <input type="hidden" name="csrf_token" value="<?= $csrf ?>">
        <input type="hidden" name="comment" value="1">
        <div class="form-row">
          <div class="form-group">
            <input type="text" name="commenter_name" class="form-control" placeholder="আপনার নাম" required>
          </div>
          <div class="form-group">
            <input type="text" name="comment_text" class="form-control" placeholder="মন্তব্য লিখুন..." required>
          </div>
        </div>
        <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-paper-plane"></i> মন্তব্য করুন</button>
      </form>
      <?php foreach ($comments as $cm): ?>
      <div class="comment">
        <span class="comment-name"><?= e($cm['commenter_name']) ?></span>
        <span class="comment-time"> • <?= bnTimeAgo($cm['created_at']) ?></span>
        <p style="margin-top:6px;"><?= e($cm['comment_text']) ?></p>
      </div>
      <?php endforeach; ?>
    </div>
  </div>
</section>

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