<?php
require_once 'includes/config.php';
require_once 'includes/DeltaConverter.php';
require_once 'includes/Stats.php';

$config = Config::load();
$about = $config['about'] ?? [
    'title' => 'À propos',
    'content' => '',
    'background' => ''
];

// Charger les statistiques
$stats = new Stats();
$siteStats = $stats->getStats();

// Fonction pour convertir le contenu Delta en HTML
function deltaToHtml($content) {
    return DeltaConverter::toHtml($content);
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($about['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">
</head>
<body>
    <!-- En-tête avec image de fond -->
    <header class="novel-header">
        <?php if (!empty($about['background'])): ?>
            <div class="novel-header-background" style="background-image: url('<?= htmlspecialchars($about['background']) ?>');"></div>
        <?php endif; ?>
        <h1><?= htmlspecialchars($about['title']) ?></h1>
    </header>

    <!-- Contenu principal -->
    <div class="novel-content">
        <div class="novel-description">
            <?= deltaToHtml($about['content']) ?>
        </div>

        <aside class="stats-menu">
            <h2>Statistiques</h2>
            <ul class="stats-list">
                <!-- Nombre total de romans -->
                <li class="stats-item">
                    <div class="stats-label">Romans publiés</div>
                    <div class="stats-value"><?= Stats::formatNumber($siteStats['total_stories']) ?></div>
                </li>

                <!-- Nombre total de chapitres -->
                <li class="stats-item">
                    <div class="stats-label">Chapitres écrits</div>
                    <div class="stats-value"><?= Stats::formatNumber($siteStats['total_chapters']) ?></div>
                    <div class="stats-detail">
                        Moyenne de <?= $siteStats['avg_chapters_per_story'] ?> chapitres par roman
                    </div>
                </li>

                <!-- Nombre total de mots -->
                <li class="stats-item">
                    <div class="stats-label">Mots écrits</div>
                    <div class="stats-value"><?= Stats::formatNumber($siteStats['total_words']) ?></div>
                    <div class="stats-detail">
                        Moyenne de <?= Stats::formatNumber($siteStats['avg_words_per_chapter']) ?> mots par chapitre
                    </div>
                </li>

                <!-- Roman avec le plus de chapitres -->
                <?php if ($siteStats['most_chapters']['story']): ?>
                <li class="stats-item">
                    <div class="stats-label">Plus long roman</div>
                    <div class="stats-value"><?= Stats::formatNumber($siteStats['most_chapters']['count']) ?> chapitres</div>
                    <div class="stats-detail">
                        <a href="roman.php?id=<?= htmlspecialchars($siteStats['most_chapters']['story']['id']) ?>">
                            <?= htmlspecialchars($siteStats['most_chapters']['story']['title']) ?>
                        </a>
                    </div>
                </li>
                <?php endif; ?>

                <!-- Plus long chapitre -->
                <?php if ($siteStats['longest_chapter']['story']): ?>
                <li class="stats-item">
                    <div class="stats-label">Plus long chapitre</div>
                    <div class="stats-value"><?= Stats::formatNumber($siteStats['longest_chapter']['words']) ?> mots</div>
                    <div class="stats-detail">
                        <a href="roman.php?id=<?= htmlspecialchars($siteStats['longest_chapter']['story']['id']) ?>">
                            <?= htmlspecialchars($siteStats['longest_chapter']['story']['title']) ?>
                        </a>
                    </div>
                </li>
                <?php endif; ?>

                <!-- Dernière mise à jour -->
                <?php if ($siteStats['latest_update']['story']): ?>
                <li class="stats-item">
                    <div class="stats-label">Dernière mise à jour</div>
                    <div class="stats-value"><?= Stats::formatDate($siteStats['latest_update']['date']) ?></div>
                    <div class="stats-detail">
                        <a href="roman.php?id=<?= htmlspecialchars($siteStats['latest_update']['story']['id']) ?>">
                            <?= htmlspecialchars($siteStats['latest_update']['story']['title']) ?>
                        </a>
                    </div>
                </li>
                <?php endif; ?>
            </ul>
        </aside>
    </div>

    <div class="back-to-home">
        <a href="index.php">&larr; Retour à l'accueil</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');
        
        window.addEventListener('scroll', function() {
            if (window.pageYOffset > 300) {
                scrollTopBtn.classList.add('visible');
            } else {
                scrollTopBtn.classList.remove('visible');
            }
        });
        
        scrollTopBtn.addEventListener('click', function() {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });
    });
    </script>
</body>
</html>