<?php
declare(strict_types=1);

require_once __DIR__ . '/../auth.php';
require_once __DIR__ . '/../db.php';

startSecureSession();

// 🎀 Gestion du changement de langue depuis l'URL (FR/EN) 🎀
if (isset($_GET['lang'])) {
    $requestedLang = strtolower((string)$_GET['lang']);
    if (in_array($requestedLang, ['fr', 'en'], true)) {
        $_SESSION['lang'] = $requestedLang;
    }
}

// 🎀 Langue active conservee en session, FR par defaut 🎀
$lang = isset($_SESSION['lang']) && in_array($_SESSION['lang'], ['fr', 'en'], true)
    ? (string)$_SESSION['lang']
    : 'fr';

// 🎀 Dictionnaire de traduction pour la navbar 🎀
$translations = [
    'fr' => [
        'memories'     => 'Mes souvenirs',
        'create_story' => 'Creer ton histoire',
        'about'        => 'Qui sommes-nous?',
        'account'      => 'Mon compte',
        'greeting'     => 'Bonjour',
        'profile'      => 'Profil',
        'logout'       => 'Deconnexion',
        'login'        => 'Connexion',
        'language'     => 'Changer la langue',
    ],
    'en' => [
        'memories'     => 'My memories',
        'create_story' => 'Create your story',
        'about'        => 'About us',
        'account'      => 'My account',
        'greeting'     => 'Hello',
        'profile'      => 'Profile',
        'logout'       => 'Log out',
        'login'        => 'Log in',
        'language'     => 'Change language',
    ],
];

$t = $translations[$lang];

// 🎀 Etat utilisateur pour adapter la navbar (connecte/non connecte) 🎀
$isLoggedIn = isUserLoggedIn();
$userName   = $isLoggedIn && isset($_SESSION['user_name']) ? (string)$_SESSION['user_name'] : '';

// 🎀 Souvenirs par défaut (affiches si BDD vide ou utilisateur non connecte) 🎀
$souvenirsFallback = [
    ['image' => '../../image/musée.jpg',             'id' => '1', 'nom' => 'Musée', 'visibility' => 'public'],
    ['image' => '../../image/temple.jpg',            'id' => '2', 'nom' => 'Temple', 'visibility' => 'public'],
    ['image' => '../../image/maison d\'enfance.jpg', 'id' => '3', 'nom' => "Maison d'enfance", 'visibility' => 'public'],
    ['image' => '../../image/planet.jpg',            'id' => '4', 'nom' => 'Planète imaginaire', 'visibility' => 'public'],
    ['image' => '../../image/dinausaure.jpg',        'id' => '5', 'nom' => 'Espace futuriste', 'visibility' => 'public'],
    ['image' => '../../image/paris.jpg',             'id' => '6', 'nom' => 'Pays ou ville', 'visibility' => 'public'],
    ['image' => '../../image/classe école .jpg',     'id' => '7', 'nom' => 'Ecole', 'visibility' => 'public'],
];

// 🎀 Chargement des souvenirs depuis la BDD si l'utilisateur est connecte 🎀
$souvenirs = $isLoggedIn ? [] : $souvenirsFallback;
$souvenirsAreEditable = $isLoggedIn;
if ($isLoggedIn && isset($_SESSION['user_id'])) {
    try {
        $pdo  = getPdo();

        $columnStmt = $pdo->query("SHOW COLUMNS FROM souvenirs LIKE 'visibility'");
        $visibilityColumn = $columnStmt ? $columnStmt->fetch(PDO::FETCH_ASSOC) : false;
        if (!$visibilityColumn) {
            $pdo->exec("ALTER TABLE souvenirs ADD COLUMN visibility ENUM('private', 'public') NOT NULL DEFAULT 'private' AFTER image");
        }

        $stmt = $pdo->prepare(
            'SELECT id, nom, description, image, visibility
               FROM souvenirs
              WHERE user_id = :user_id
              ORDER BY created_at DESC'
        );
        $stmt->execute(['user_id' => (int)$_SESSION['user_id']]);
        $souvenirs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    } catch (Throwable $e) {
        $souvenirs = [];
    }
}
?>
<!DOCTYPE html>
<html lang="<?= htmlspecialchars($lang, ENT_QUOTES, 'UTF-8') ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MindGallery</title>
    <link rel="icon"       href="../../image/logo3.png" type="image/png">
    <link rel="stylesheet" href="../../css/style.css">
    <link rel="stylesheet" href="../../css/style-accueil.css">
    <link rel="stylesheet" href="../../css/style-souvenir.css">
    <link rel="stylesheet" href="../../css/style-detail.css">
    <?php if (isset($extraStyles) && is_array($extraStyles)): ?>
        <?php foreach ($extraStyles as $stylePath): ?>
            <link rel="stylesheet" href="<?= htmlspecialchars((string)$stylePath, ENT_QUOTES, 'UTF-8') ?>">
        <?php endforeach; ?>
    <?php endif; ?>
</head>
<body>
<header>
    <section class="navbar">
        <div class="logo">
            <a href="../index.php">
                <img src="../../image/logo3.png" alt="MindGallery Logo" class="logo-image">
                MindGallery
            </a>
        </div>

        <!-- 🎀 Checkbox cachee pour l'etat du menu burger 🎀 -->
        <input type="checkbox" id="menu-toggle" class="menu-toggle">

        <!-- 🎀 Bouton burger 🎀 -->
        <label for="menu-toggle" class="burger">
            <span class="burger-line"></span>
            <span class="burger-line"></span>
            <span class="burger-line"></span>
        </label>

        <nav>
            <ul>
                <li><a href="liste-souvenirs.php"><?= htmlspecialchars($t['memories'],     ENT_QUOTES, 'UTF-8') ?></a></li>
                <li><a href="ajout-souvenir.php"><?=  htmlspecialchars($t['create_story'], ENT_QUOTES, 'UTF-8') ?></a></li>
                <li><a href="../index.php#about"><?=  htmlspecialchars($t['about'],        ENT_QUOTES, 'UTF-8') ?></a></li>
            </ul>

            <?php if ($isLoggedIn): ?>
                <!-- 🎀 Sous-menu compte 🎀 -->
                <details class="account-switch">
                    <summary class="account-icon"
                             aria-label="<?= htmlspecialchars($t['account'], ENT_QUOTES, 'UTF-8') ?>"
                             title="<?= htmlspecialchars($t['account'], ENT_QUOTES, 'UTF-8') ?>">
                        <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
                            <path d="M12 12a5 5 0 1 0-5-5 5 5 0 0 0 5 5Zm0 2c-4.42 0-8 2.24-8 5v1h16v-1c0-2.76-3.58-5-8-5Z"/>
                        </svg>
                    </summary>
                    <div class="account-menu">
                        <p class="account-label">
                            <?= htmlspecialchars($t['greeting'], ENT_QUOTES, 'UTF-8') ?>
                            <?= htmlspecialchars($userName,      ENT_QUOTES, 'UTF-8') ?>
                        </p>
                        <a class="account-option" href="../profile.php"><?= htmlspecialchars($t['profile'], ENT_QUOTES, 'UTF-8') ?></a>
                        <a class="account-option" href="../logout.php"><?=  htmlspecialchars($t['logout'],  ENT_QUOTES, 'UTF-8') ?></a>
                    </div>
                </details>
            <?php else: ?>
                <a class="login-icon" href="../login.php"
                   aria-label="<?= htmlspecialchars($t['login'], ENT_QUOTES, 'UTF-8') ?>"
                   title="<?= htmlspecialchars($t['login'], ENT_QUOTES, 'UTF-8') ?>">
                    <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
                        <path d="M12 12a5 5 0 1 0-5-5 5 5 0 0 0 5 5Zm0 2c-4.42 0-8 2.24-8 5v1h16v-1c0-2.76-3.58-5-8-5Z"/>
                    </svg>
                </a>
            <?php endif; ?>

            <!-- 🎀 Selecteur de langue PHP (remplace Google Translate) 🎀 -->
            <details class="lang-switch">
                <summary class="lang-icon"
                         aria-label="<?= htmlspecialchars($t['language'], ENT_QUOTES, 'UTF-8') ?>"
                         title="<?= htmlspecialchars($t['language'], ENT_QUOTES, 'UTF-8') ?>">
                    <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
                        <path d="M12 2a10 10 0 1 0 10 10A10 10 0 0 0 12 2Zm6.9 9h-3.03a15.7 15.7 0 0 0-1.24-5A8.02 8.02 0 0 1 18.9 11ZM12 4.05A13.8 13.8 0 0 1 13.86 11h-3.72A13.8 13.8 0 0 1 12 4.05ZM4.1 13h3.03a15.7 15.7 0 0 0 1.24 5A8.02 8.02 0 0 1 4.1 13Zm3.03-2H4.1a8.02 8.02 0 0 1 4.27-5 15.7 15.7 0 0 0-1.24 5ZM12 19.95A13.8 13.8 0 0 1 10.14 13h3.72A13.8 13.8 0 0 1 12 19.95ZM13 13h3.95a13.8 13.8 0 0 1-1.13 4.02A8 8 0 0 1 13 13Zm3.95-2H13a8 8 0 0 1 2.82-4.02A13.8 13.8 0 0 1 16.95 11ZM11 13a8 8 0 0 1-2.82 4.02A13.8 13.8 0 0 1 7.05 13Zm-3.95-2a13.8 13.8 0 0 1 1.13-4.02A8 8 0 0 1 11 11Z"/>
                    </svg>
                </summary>
                <div class="lang-menu">
                    <a class="lang-option <?= $lang === 'fr' ? 'is-active' : '' ?>" href="?lang=fr">FR</a>
                    <a class="lang-option <?= $lang === 'en' ? 'is-active' : '' ?>" href="?lang=en">EN</a>
                </div>
            </details>
        </nav>
    </section>
</header>