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

// Récupération des paramètres
$storyId = $_GET['story'] ?? '';
$chapterId = $_GET['chapter'] ?? '';

if (!$storyId || !$chapterId) {
    header('Location: index.php');
    exit;
}

// Chargement du roman
$story = Stories::get($storyId);
if (!$story) {
    header('Location: index.php');
    exit;
}

// Recherche du chapitre
$currentChapter = null;
$currentIndex = -1;
foreach ($story['chapters'] as $index => $chapter) {
    if ($chapter['id'] === $chapterId) {
        $currentChapter = $chapter;
        $currentIndex = $index;
        break;
    }
}

if (!$currentChapter) {
    header('Location: roman.php?id=' . urlencode($storyId));
    exit;
}

// Récupération des chapitres précédent et suivant
$prevChapter = $currentIndex > 0 ? $story['chapters'][$currentIndex - 1] : null;
$nextChapter = $currentIndex < count($story['chapters']) - 1 ? $story['chapters'][$currentIndex + 1] : null;

$config = Config::load();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($currentChapter['title']) ?> - <?= htmlspecialchars($story['title']) ?> - <?= htmlspecialchars($config['site']['name']) ?></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/public.css">
    <link rel="stylesheet" href="assets/css/content.css">
    
    <meta name="description" content="<?= htmlspecialchars($story['title']) ?> - <?= htmlspecialchars($currentChapter['title']) ?>">
</head>
<body>
    <!-- En-tête avec image de couverture si disponible -->
    <header class="novel-header">
        <?php if (!empty($currentChapter['cover'])): ?>
            <div class="novel-header-background" style="background-image: url('<?= htmlspecialchars($currentChapter['cover']) ?>');"></div>
        <?php endif; ?>
        <h1><?= htmlspecialchars($currentChapter['title']) ?></h1>
    </header>

    <!-- Contenu principal -->
    <div class="novel-content">
        <div class="novel-description chapter-content">
            <?= $currentChapter['content'] ?>
            
            <!-- Navigation entre chapitres -->
            <div class="chapter-navigation">
                <?php if ($prevChapter): ?>
                    <a href="?story=<?= urlencode($storyId) ?>&chapter=<?= urlencode($prevChapter['id']) ?>" 
                       class="chapter-nav prev-chapter">
                        &larr; <?= htmlspecialchars($prevChapter['title']) ?>
                    </a>
                <?php endif; ?>
                
                <?php if ($nextChapter): ?>
                    <a href="?story=<?= urlencode($storyId) ?>&chapter=<?= urlencode($nextChapter['id']) ?>" 
                       class="chapter-nav next-chapter">
                        <?= htmlspecialchars($nextChapter['title']) ?> &rarr;
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <aside class="chapters-menu">
            <h2>Chapitres</h2>
            <ul class="chapters-list">
                <?php foreach ($story['chapters'] as $chapter): ?>
                    <li>
                        <a href="?story=<?= urlencode($storyId) ?>&chapter=<?= urlencode($chapter['id']) ?>"
                           class="<?= $chapter['id'] === $chapterId ? 'current-chapter' : '' ?>">
                            <?= htmlspecialchars($chapter['title']) ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        </aside>
    </div>

    <div class="back-to-home">
        <a href="index.php">&larr; Retour à l'accueil</a> | 
        <a href="roman.php?id=<?= urlencode($storyId) ?>">&larr; Retour au roman</a>
    </div>

    <button class="scroll-top" aria-label="Retour en haut de page">↑</button>

    <script>
    document.addEventListener('DOMContentLoaded', function() {
        const scrollTopBtn = document.querySelector('.scroll-top');
        
        // Afficher/masquer le bouton de retour en haut
        window.addEventListener('scroll', function() {
            if (window.pageYOffset > 300) {
                scrollTopBtn.classList.add('visible');
            } else {
                scrollTopBtn.classList.remove('visible');
            }
        });
        
        // Action de retour en haut
        scrollTopBtn.addEventListener('click', function() {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });
    });
    </script>
</body>
</html>