<?php
require_once __DIR__ . '/header.php';

if (!$isLoggedIn) {
    header('Location: ../login.php');
    exit;
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id === 0) {
    header('Location: liste-souvenirs.php');
    exit;
}

// 🎀 Charge le souvenir (thème) depuis la BDD 🎀
try {
    $pdo  = getPdo();

    // Le souvenir principal (n'importe quel user_id pour le thème)
    $stmt = $pdo->prepare('SELECT * FROM souvenirs WHERE id = :id LIMIT 1');
    $stmt->execute(['id' => $id]);
    $souvenir = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
    $souvenir = null;
}

if (!$souvenir) {
    header('Location: liste-souvenirs.php');
    exit;
}

// 🎀 Mes souvenirs de ce thème (privés ET publics) 🎀
try {
    $stmtMes = $pdo->prepare(
        "SELECT * FROM souvenirs
          WHERE user_id = :uid AND nom = :nom
          ORDER BY created_at DESC"
    );
    $stmtMes->execute(['uid' => (int)$_SESSION['user_id'], 'nom' => $souvenir['nom']]);
    $mesSouvenirs = $stmtMes->fetchAll(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
    $mesSouvenirs = [];
}

// 🎀 Souvenirs PUBLIC des amis sur ce thème uniquement 🎀
try {
    $stmtCommuns = $pdo->prepare(
        "SELECT s.*, u.name AS ami_name
           FROM souvenirs s
           JOIN amis a ON (
               (a.user_id = :uid AND a.ami_id = s.user_id)
               OR (a.ami_id = :uid2 AND a.user_id = s.user_id)
           )
           JOIN users u ON u.id = s.user_id
          WHERE s.nom = :nom AND a.statut = 'accepte' AND s.visibility = 'public'
          ORDER BY s.created_at DESC"
    );
    $stmtCommuns->execute([
        'uid'  => (int)$_SESSION['user_id'],
        'uid2' => (int)$_SESSION['user_id'],
        'nom'  => $souvenir['nom'],
    ]);
    $souvenirCommuns = $stmtCommuns->fetchAll(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
    $souvenirCommuns = [];
}

?>

<main class="theme-main">

    <!-- ─── HERO ─── -->
    <div class="theme-hero">
        <img src="<?= htmlspecialchars($souvenir['image'], ENT_QUOTES, 'UTF-8') ?>"
             alt="<?= htmlspecialchars($souvenir['nom'],   ENT_QUOTES, 'UTF-8') ?>"
             class="theme-hero-img">
        <div class="theme-hero-overlay"></div>
        <div class="theme-hero-content">
            <a href="liste-souvenirs.php" class="theme-back">
                <svg viewBox="0 0 24 24" fill="none"><path d="M15 18l-6-6 6-6" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
                Retour
            </a>
            <h1 class="theme-title"><?= htmlspecialchars($souvenir['nom'], ENT_QUOTES, 'UTF-8') ?></h1>
        </div>
    </div>

    <!-- ─── TABS ─── -->
    <div class="theme-tabs">
        <a href="#mes-souvenirs"  class="theme-tab active">Mes souvenirs</a>
        <a href="amis-souvenir.php?theme=<?= urlencode($souvenir['nom']) ?>" class="theme-tab theme-tab-icon" aria-label="Amis">
            <svg viewBox="0 0 24 24" fill="none">
                <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                <circle cx="9" cy="7" r="4" stroke="currentColor" stroke-width="2"/>
                <path d="M23 21v-2a4 4 0 0 0-3-3.87M16 3.13a4 4 0 0 1 0 7.75" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
            </svg>
        </a>
        <a href="#nos-souvenirs" class="theme-tab">Nos souvenirs</a>
    </div>

    <div class="separation"></div>

    <!-- ─── MES SOUVENIRS ─── -->
    <section class="theme-section" id="mes-souvenirs">
        <h2 class="theme-section-title">Mes souvenirs</h2>
        <?php if (empty($mesSouvenirs)): ?>
            <p class="theme-empty">Tu n'as pas encore de souvenir privé pour ce thème.
                <a href="ajout-souvenir.php">Ajouter</a>
            </p>
        <?php else: ?>
            <div class="theme-grid">
                <?php foreach ($mesSouvenirs as $s): ?>
                    <div class="theme-card">
                        <div class="theme-card-img-wrap">
                            <img src="<?= htmlspecialchars($s['image'], ENT_QUOTES, 'UTF-8') ?>"
                                 alt="<?= htmlspecialchars($s['nom'],   ENT_QUOTES, 'UTF-8') ?>"
                                 class="theme-card-img">
                            <div class="theme-card-overlay"></div>
                        </div>
                        <div class="theme-card-bar">
                            <span class="theme-card-name"><?= htmlspecialchars($s['nom'], ENT_QUOTES, 'UTF-8') ?></span>
                            <span class="theme-card-badge private">Privé</span>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>

    <div class="separation"></div>

    <!-- ─── NOS SOUVENIRS (communs avec amis) ─── -->
    <section class="theme-section" id="nos-souvenirs">
        <h2 class="theme-section-title">Nos souvenirs</h2>
        <?php if (empty($souvenirCommuns)): ?>
            <p class="theme-empty">Aucun souvenir en commun avec tes amis pour ce thème.
                <a href="amis-souvenir.php?theme=<?= urlencode($souvenir['nom']) ?>">Voir mes amis</a>
            </p>
        <?php else: ?>
            <div class="theme-grid">
                <?php foreach ($souvenirCommuns as $s): ?>
                    <div class="theme-card">
                        <div class="theme-card-img-wrap">
                            <img src="<?= htmlspecialchars($s['image'], ENT_QUOTES, 'UTF-8') ?>"
                                 alt="<?= htmlspecialchars($s['nom'],   ENT_QUOTES, 'UTF-8') ?>"
                                 class="theme-card-img">
                            <div class="theme-card-overlay"></div>
                        </div>
                        <div class="theme-card-bar">
                            <span class="theme-card-name"><?= htmlspecialchars($s['nom'], ENT_QUOTES, 'UTF-8') ?></span>
                            <span class="theme-card-ami"><?= htmlspecialchars($s['ami_name'], ENT_QUOTES, 'UTF-8') ?></span>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>

</main>

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