<?php
require_once '../includes/config.php';
require_once '../includes/auth.php';
require_once '../includes/stories.php';

// Vérification de l'authentification
if (!Auth::check()) {
    header('Location: login.php');
    exit;
}

$stories = Stories::getAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Administration</title>
    <?php if (file_exists(__DIR__ . '/../assets/images/site/favicon.png')): ?>
        <link rel="icon" type="image/png" href="../assets/images/site/favicon.png">
    <?php endif; ?>
    <link rel="stylesheet" href="../assets/css/main.css">
    <style>
        .version-banner {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 8px;
            background-color: var(--bg-tertiary);
            color: var(--text-secondary);
            text-align: center;
            font-size: 0.8rem;
            border-top: 1px solid var(--border-color);
            z-index: 100;
        }
    </style>
</head>
<body>
    <nav class="admin-nav">
        <div class="nav-brand">
            <?php
            $config = Config::load();
            if (!empty($config['site']['logo'])): ?>
                <img src="<?= htmlspecialchars('../' . $config['site']['logo']) ?>" 
                    alt="<?= htmlspecialchars($config['site']['name']) ?>">
            <?php endif; ?>
            <span>Administration</span>
        </div>
        <div class="nav-menu">
            <a href="../index.php" target="_blank" class="button">Visiter le site</a>
            <a href="profile.php" class="button">Profil</a>
            <a href="story-edit.php" class="button">Nouveau roman</a>
            <a href="options.php" class="button">Options</a>
            <a href="export-import.php" class="button">Import/Export</a>
            <form method="POST" action="logout.php" class="logout-form">
                <button type="submit">Déconnexion</button>
            </form>
        </div>
    </nav>

    <main class="admin-main">
        <h1>Gestion des romans</h1>
        
        <div class="stories-list">
            <?php foreach ($stories as $story): ?>
            <div class="story-item">
                <img src="<?= htmlspecialchars('../' . $story['cover']) ?>" alt="" class="story-cover">
                <div class="story-info">
                    <h2><?= htmlspecialchars($story['title']) ?></h2>
                    <p>
                        <?= count($story['chapters'] ?? []) ?> chapitre<?= count($story['chapters'] ?? []) > 1 ? 's' : '' ?><br>
                        Dernière modification : <?= htmlspecialchars(Stories::formatDate($story['updated'])) ?>
                    </p>
                </div>
                <div class="story-actions">
                    <a href="story-edit.php?id=<?= htmlspecialchars($story['id']) ?>" class="button">Modifier</a>
                    <button type="button" class="button delete-story" data-id="<?= htmlspecialchars($story['id']) ?>">Supprimer</button>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </main>

    <script src="../assets/js/admin.js"></script>
    <link rel="stylesheet" href="../assets/css/dialog.css">
    <script src="../assets/js/dialog.js"></script>

    <?php
    $version = file_exists(__DIR__ . '/../version.txt') 
        ? trim(file_get_contents(__DIR__ . '/../version.txt'))
        : 'version inconnue';
    ?>
    <div class="version-banner">
        Lectures d'Esenjin - <a href="https://git.crystalyx.net/Esenjin_Asakha/Lectures" target="_blank" style="color: inherit; text-decoration: underline;">v.<?= htmlspecialchars($version) ?></a>
    </div>
</body>
</html>