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

$config = Config::load();
$stories = Stories::getAll();

// Trier les romans par date de mise à jour (du plus récent au plus ancien)
usort($stories, function($a, $b) {
    return strtotime($b['updated']) - strtotime($a['updated']);
});

// Fonction pour vérifier si un roman est une nouvelle parution (moins d'une semaine)
function isNewRelease($story) {
    $updateTime = strtotime($story['updated']);
    $weekAgo = strtotime('-1 week');
    return $updateTime > $weekAgo;
}

// Fonction pour formater la date en français
function formatDate($date) {
    $timestamp = strtotime($date);
    $months = [
        'janvier', 'février', 'mars', 'avril', 'mai', 'juin',
        'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'
    ];
    
    return sprintf(
        "%d %s %d",
        date('j', $timestamp),
        $months[date('n', $timestamp) - 1],
        date('Y', $timestamp)
    );
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <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">
    
    <meta name="description" content="<?= htmlspecialchars($config['site']['description']) ?>">
</head>
<body>
    <div class="header-container">
        <header class="site-header">
            <?php if (!empty($config['site']['logo'])): ?>
                <img src="<?= htmlspecialchars($config['site']['logo']) ?>" 
                    alt="<?= htmlspecialchars($config['site']['name']) ?>"
                    class="site-logo">
            <?php endif; ?>
            
            <div class="site-header-content">
                <h1><?= htmlspecialchars($config['site']['name']) ?></h1>
                <p><?= nl2br(htmlspecialchars($config['site']['description'])) ?></p>
            </div>
        </header>
    </div>

    <main class="main-content">
        <div class="novels-grid">
            <?php foreach ($stories as $story): ?>
                <a href="roman.php?id=<?= htmlspecialchars($story['id']) ?>" class="novel-card <?= isNewRelease($story) ? 'new-release' : '' ?>">
                    <img src="<?= htmlspecialchars($story['cover']) ?>" 
                        alt="Couverture de <?= htmlspecialchars($story['title']) ?>"
                        class="novel-cover"
                        loading="lazy">
                        
                    <div class="novel-info">
                        <h2><?= htmlspecialchars($story['title']) ?></h2>
                        <p>
                            <?= count($story['chapters'] ?? []) ?> 
                            chapitre<?= count($story['chapters'] ?? []) > 1 ? 's' : '' ?>
                        </p>
                        <div class="novel-date">
                            Mis à jour le <?= formatDate($story['updated']) ?>
                        </div>
                    </div>
                </a>
            <?php endforeach; ?>
        </div>
    </main>
</body>
</html>