<?php
require_once __DIR__ . '/config.php';

$page     = max(1, (int)($_GET['page'] ?? 1));
$category = trim($_GET['cat'] ?? '');
$offset   = ($page - 1) * BLOG_PER_PAGE;

$db = get_db();

// Fetch articles
$where  = "status = 'published'";
$params = [];
if ($category) {
    $where   .= " AND category = :cat";
    $params[':cat'] = $category;
}

$total = $db->prepare("SELECT COUNT(*) FROM articles WHERE $where");
$total->execute($params);
$totalCount = (int)$total->fetchColumn();
$totalPages = (int)ceil($totalCount / BLOG_PER_PAGE);

$stmt = $db->prepare("SELECT id, slug, title, excerpt, category, cover_image, reading_time, views, featured, published_at
                       FROM articles WHERE $where
                       ORDER BY featured DESC, published_at DESC
                       LIMIT :limit OFFSET :offset");
$stmt->bindValue(':limit',  BLOG_PER_PAGE, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset,       PDO::PARAM_INT);
foreach ($params as $k => $v) $stmt->bindValue($k, $v);
$stmt->execute();
$articles = $stmt->fetchAll();

// Categories with counts
$cats = $db->query("SELECT category, COUNT(*) as cnt FROM articles WHERE status='published' GROUP BY category ORDER BY cnt DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
  <title>Blog — Lumière Blanche</title>
  <meta name="description" content="Articles spirituels sur le tarot, l'astrologie, les chakras et le développement de l'âme."/>
  <link rel="preconnect" href="https://fonts.googleapis.com"/>
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
  <link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,300;0,400;0,600;1,300;1,400&family=Cinzel+Decorative:wght@400;700&family=Raleway:wght@200;300;400&display=swap" rel="stylesheet"/>
  <link rel="stylesheet" href="../css/style.css"/>
  <link rel="stylesheet" href="blog.css"/>
</head>
<body class="blog-page">
<canvas id="starCanvas"></canvas>

<nav class="nav" id="nav">
  <div class="nav-logo">
    <a href="../index.html">
      <span class="nav-logo-symbol">✦</span>
      <span class="nav-logo-text">Lumière Blanche</span>
    </a>
  </div>
  <ul class="nav-links">
    <li><a href="../index.html" class="nav-link">Accueil</a></li>
    <li><a href="../index.html#tarifs" class="nav-link">Tarifs</a></li>
    <li><a href="index.php" class="nav-link active">Blog</a></li>
    <li><a href="../index.html#contact" class="nav-link nav-link--cta">Réserver</a></li>
  </ul>
</nav>

<header class="blog-hero">
  <div class="blog-hero-orb"></div>
  <div class="blog-hero-content">
    <span class="section-label section-label--center">Sagesse & Lumière</span>
    <h1 class="blog-hero-title">Le Blog</h1>
    <p class="blog-hero-sub">Articles spirituels, guidance et explorations de l'âme</p>
  </div>
</header>

<main class="blog-main container">
  <div class="blog-layout">

    <section class="blog-grid-section">

      <?php if ($category): ?>
      <div class="blog-filter-active">
        <span>Catégorie : <strong><?= e($category) ?></strong></span>
        <a href="index.php" class="blog-filter-clear">✕ Tout voir</a>
      </div>
      <?php endif; ?>

      <?php if (empty($articles)): ?>
      <div class="blog-empty">
        <span>✦</span>
        <p>Aucun article dans cette catégorie pour le moment.</p>
        <a href="index.php" class="btn btn--ghost">Voir tous les articles</a>
      </div>
      <?php else: ?>
      <div class="blog-grid">
        <?php foreach ($articles as $a): ?>
        <article class="blog-card <?= $a['featured'] ? 'blog-card--featured' : '' ?>">
          <a href="article.php?slug=<?= e($a['slug']) ?>" class="blog-card-link">
            <div class="blog-card-cover">
              <?php if ($a['cover_image']): ?>
              <img src="<?= e($a['cover_image']) ?>" alt="<?= e($a['title']) ?>" loading="lazy"/>
              <?php else: ?>
              <div class="blog-card-cover-placeholder">
                <span>✦</span>
              </div>
              <?php endif; ?>
              <span class="blog-card-category"><?= e($a['category']) ?></span>
              <?php if ($a['featured']): ?>
              <span class="blog-card-featured-badge">À la une</span>
              <?php endif; ?>
            </div>
            <div class="blog-card-body">
              <h2 class="blog-card-title"><?= e($a['title']) ?></h2>
              <p class="blog-card-excerpt"><?= e($a['excerpt']) ?></p>
              <div class="blog-card-meta">
                <span>✦ <?= e(date('d M Y', strtotime($a['published_at']))) ?></span>
                <span>◈ <?= $a['reading_time'] ?> min de lecture</span>
                <span>◎ <?= number_format($a['views']) ?> vues</span>
              </div>
            </div>
          </a>
        </article>
        <?php endforeach; ?>
      </div>

      <?php if ($totalPages > 1): ?>
      <nav class="blog-pagination">
        <?php if ($page > 1): ?>
        <a href="?page=<?= $page-1 ?><?= $category ? '&cat='.urlencode($category) : '' ?>" class="blog-page-btn">← Précédent</a>
        <?php endif; ?>
        <span class="blog-page-info">Page <?= $page ?> / <?= $totalPages ?></span>
        <?php if ($page < $totalPages): ?>
        <a href="?page=<?= $page+1 ?><?= $category ? '&cat='.urlencode($category) : '' ?>" class="blog-page-btn">Suivant →</a>
        <?php endif; ?>
      </nav>
      <?php endif; ?>
      <?php endif; ?>
    </section>

    <aside class="blog-sidebar">
      <div class="sidebar-widget">
        <h3 class="sidebar-title">Catégories</h3>
        <ul class="sidebar-cats">
          <li><a href="index.php" class="<?= !$category ? 'active' : '' ?>">Tous les articles <span><?= $totalCount ?></span></a></li>
          <?php foreach ($cats as $c): ?>
          <li>
            <a href="?cat=<?= urlencode($c['category']) ?>" class="<?= $category === $c['category'] ? 'active' : '' ?>">
              <?= e($c['category']) ?> <span><?= $c['cnt'] ?></span>
            </a>
          </li>
          <?php endforeach; ?>
        </ul>
      </div>

      <div class="sidebar-widget sidebar-widget--cta">
        <span class="sidebar-cta-symbol">✦</span>
        <h3>Prête à aller plus loin ?</h3>
        <p>Réservez une consultation personnalisée et recevez votre guidance en direct.</p>
        <a href="../index.html#reservation" class="btn btn--primary btn--full">
          <span>Réserver</span>
          <span class="btn-shimmer"></span>
        </a>
      </div>
    </aside>
  </div>
</main>

<footer class="blog-footer">
  <p>© <?= date('Y') ?> <?= SITE_NAME ?> · <a href="../index.html">Retour au site</a></p>
</footer>

<script src="../js/config.js"></script>
<script src="../js/blog-stars.js"></script>
</body>
</html>
