<?php

declare(strict_types=1);

$extraStyles = ['../../css/style-ajout.css'];
require_once __DIR__ . '/header.php';

if (!isUserLoggedIn() || !isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit;
}

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

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 id = :id AND user_id = :user_id LIMIT 1');
    $stmt->execute([
        'id' => $id,
        'user_id' => (int)$_SESSION['user_id'],
    ]);
    $souvenir = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
    header('Location: liste-souvenirs.php?error=' . urlencode('Impossible de charger le souvenir.'));
    exit;
}

if (!$souvenir) {
    header('Location: liste-souvenirs.php?error=' . urlencode('Souvenir introuvable.'));
    exit;
}

$error = isset($_GET['error']) ? trim((string)$_GET['error']) : '';
?>

<main class="add-memory-main">
    <form action="traitement-modifier-souvenir.php" method="post" class="add-memory-form" enctype="multipart/form-data">
        <h1>Modifier le souvenir</h1>

        <?php if ($error !== ''): ?>
            <p class="add-memory-message add-memory-error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></p>
        <?php endif; ?>

        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrfToken(), ENT_QUOTES, 'UTF-8') ?>">
        <input type="hidden" name="id" value="<?= htmlspecialchars((string)$souvenir['id'], ENT_QUOTES, 'UTF-8') ?>">

        <label for="nom">Nom du souvenir</label>
        <input
            id="nom"
            name="nom"
            type="text"
            placeholder="Nom du souvenir"
            maxlength="120"
            value="<?= htmlspecialchars((string)$souvenir['nom'], ENT_QUOTES, 'UTF-8') ?>"
            required
        >

        <label for="description">Description</label>
        <textarea id="description" name="description" placeholder="Decris ton souvenir..." required><?= htmlspecialchars((string)$souvenir['description'], ENT_QUOTES, 'UTF-8') ?></textarea>

        <label for="visibility">Visibilite</label>
        <select id="visibility" name="visibility" required>
            <option value="private" <?= ((string)$souvenir['visibility'] === 'private') ? 'selected' : '' ?>>Prive</option>
            <option value="public" <?= ((string)$souvenir['visibility'] === 'public') ? 'selected' : '' ?>>Public</option>
        </select>

        <label>Image actuelle</label>
        <img src="<?= htmlspecialchars((string)$souvenir['image'], ENT_QUOTES, 'UTF-8') ?>" alt="Image actuelle du souvenir" class="edit-memory-preview">

        <label for="image">Nouvelle image (optionnel)</label>
        <input id="image" name="image" type="file" accept="image/*">

        <div class="edit-memory-actions">
            <a href="liste-souvenirs.php" class="edit-memory-cancel">Annuler</a>
            <button type="submit" class="home-button">Enregistrer les modifications</button>
        </div>
    </form>
</main>

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