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

$headerStyle = 'solid'; 
$currentPage = 'articles';

// Fetch articles dynamically from database
try {
    $db = getDB();
    $stmt = $db->query("SELECT * FROM articles WHERE status = 'publie' ORDER BY id DESC");
    $articles = $stmt->fetchAll();
} catch (Exception $e) {
    $articles = [];
}

// Fallback if DB empty
if (empty($articles)) {
    $articles = [
        [
            'id' => 1,
            'slug' => 'le-marche-immobilier-de-prestige-a-sidi-rahal-casablanca-en-2025',
            'image_path' => 'assets/img/project-1.jpg',
            'category' => 'Marché Immobilier',
            'title' => 'Le marché immobilier de prestige à Sidi Rahal Casablanca en 2025',
            'excerpt' => 'Analyse approfondie des tendances, de la valorisation du littoral et des opportunités d\'investissement dans la région de Sidi Rahal Casablanca.',
            'created_at' => '2025-05-15'
        ],
        [
            'id' => 2,
            'slug' => 'acquerir-sa-villa-d-exception-a-sidi-rahal-le-guide-complet',
            'image_path' => 'assets/img/project-2.jpg',
            'category' => 'Conseils & Guide',
            'title' => 'Acquérir sa villa d\'exception à Sidi Rahal : le guide complet',
            'excerpt' => 'Les critères fondamentaux pour réussir l\'acquisition d\'une propriété haut de gamme : vérification juridique, orientation et prestations.',
            'created_at' => '2025-05-10'
        ],
        [
            'id' => 3,
            'slug' => 'investir-dans-le-littoral-casablancais-opportunites-et-rendement',
            'image_path' => 'assets/img/project-3.jpg',
            'category' => 'Investissement',
            'title' => 'Investir dans le littoral casablancais : opportunités et rendement',
            'excerpt' => 'Pourquoi la frange littorale de Sidi Rahal s\'impose comme le nouvel épicentre des résidences secondaires et locatives haut de gamme.',
            'created_at' => '2025-05-05'
        ]
    ];
}

$featuredSlides = array_slice($articles, 0, 3);
?>
<!DOCTYPE html>
<html lang="fr" class="scroll-smooth">
<head>
  <?= renderHead('Actualités & Analyses — AJOUAMAI IMMO-BUSINESS', 'Retrouvez les dernières actualités immobilières, analyses de marché et conseils d\'experts à Sidi Rahal Casablanca.') ?>
</head>
<body class="bg-offwhite text-dark font-sans">
<?php include __DIR__ . '/../components/header.php'; ?>

<!-- PAGE HEADER / BIG TITLE SECTION FIRST -->
<section class="pt-32 pb-12 bg-black text-white text-center border-b border-gray-800">
  <div class="max-w-4xl mx-auto px-6 reveal-scale">
    <p class="text-[11px] tracking-[0.35em] uppercase font-sans mb-3 text-gold font-semibold">JOURNAL & PERSPECTIVES</p>
    <h1 class="font-serif text-4xl sm:text-6xl font-light tracking-tight mb-4 text-[#D4C7A3]">Actualités & Analyses</h1>
    <div class="w-16 h-px bg-gold mx-auto mb-4"></div>
    <p class="text-sm sm:text-base font-sans text-gray-300 font-light max-w-lg mx-auto">
      Décryptage du marché immobilier haut de gamme à Sidi Rahal Casablanca et conseils d'experts.
    </p>
  </div>
</section>

<!-- AUTOMATIC HERO ARTICLE SLIDER SECTION (AFTER BIG TITLE) -->
<section class="relative h-[60vh] min-h-[480px] bg-black text-white overflow-hidden">
  <div class="swiper swiper-featured-properties w-full h-full">
    <div class="swiper-wrapper">
      <?php foreach($featuredSlides as $slide): 
        $slideImg = resolveAssetUrl($slide['image_path'] ?? $slide['img'] ?? 'assets/img/project-1.jpg');
        $slideCategory = $slide['category'] ?? $slide['cat'] ?? 'Actualité';
      ?>
        <div class="swiper-slide relative w-full h-full">
          <img src="<?= htmlspecialchars($slideImg) ?>" class="w-full h-full object-cover opacity-70" alt="<?= htmlspecialchars($slide['title']) ?>" />
          <div class="absolute inset-0 bg-gradient-to-t from-black via-black/30 to-black/60"></div>
          
          <div class="absolute bottom-12 left-0 right-0 max-w-7xl mx-auto px-6 z-10">
            <span class="bg-gold text-white text-[10px] tracking-[0.25em] uppercase font-sans px-3 py-1.5 font-semibold inline-block mb-3">
              À LA UNE · <?= htmlspecialchars($slideCategory) ?>
            </span>
            <h2 class="font-serif text-3xl sm:text-5xl font-light text-white mb-3 max-w-3xl leading-tight">
              <?= htmlspecialchars($slide['title']) ?>
            </h2>
            <p class="text-gray-300 font-sans text-sm font-light max-w-2xl mb-6 line-clamp-2">
              <?= htmlspecialchars($slide['excerpt']) ?>
            </p>
            <a href="<?= SITE_URL ?>/pages/article.php?slug=<?= htmlspecialchars($slide['slug']) ?>" class="btn-lemonde-consultation inline-flex">
              <span>Découvrir l'article</span>
              <svg class="w-4 h-4 ml-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M17.25 8.25L21 12m0 0l-3.75 3.75M21 12H3"/></svg>
            </a>
          </div>
        </div>
      <?php endforeach; ?>
    </div>

    <!-- Navigation Arrows -->
    <div class="absolute bottom-12 right-6 z-20 hidden md:flex items-center gap-2">
      <div class="swiper-button-prev-custom rounded-full cursor-pointer">←</div>
      <div class="swiper-button-next-custom rounded-full cursor-pointer">→</div>
    </div>

    <!-- Swiper Pagination Dots -->
    <div class="swiper-pagination !bottom-4"></div>
  </div>
</section>

<!-- MAIN ARTICLES GRID -->
<section class="py-24 bg-white">
  <div class="max-w-7xl mx-auto px-6">
    
    <div class="flex items-center justify-between mb-16 border-b border-gray-200 pb-4">
      <p class="text-xs tracking-[0.2em] uppercase font-sans text-gray-500">
        Publications & Analyses d'Équipe (<span class="text-black font-semibold"><?= count($articles) ?></span>)
      </p>
      <span class="text-xs text-gold font-sans uppercase tracking-widest">AJOUAMAI IMMO-BUSINESS Journal</span>
    </div>

    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 sm:gap-10">
      <?php foreach($articles as $i => $art): 
        $artImg = resolveAssetUrl($art['image_path'] ?? $art['img'] ?? 'assets/img/project-1.jpg');
        $artCat = $art['category'] ?? $art['cat'] ?? 'Actualité';
        $artDate = !empty($art['created_at']) ? date('d/m/Y', strtotime($art['created_at'])) : ($art['date'] ?? '2025');
        $artLink = SITE_URL . '/pages/article.php?slug=' . htmlspecialchars($art['slug']);
      ?>
        <div class="reveal" style="transition-delay: <?= $i * 80 ?>ms">
          <a href="<?= $artLink ?>" class="group block cursor-pointer bg-offwhite border border-gray-200/70 hover:border-gold transition-all duration-500 shadow-xs hover:shadow-xl flex flex-col h-full">
            <div class="aspect-[16/10] overflow-hidden relative">
              <img src="<?= htmlspecialchars($artImg) ?>" class="w-full h-full object-cover transition-transform duration-700 group-hover:scale-105" alt="<?= htmlspecialchars($art['title']) ?>" loading="lazy"/>
              <span class="absolute top-4 left-4 bg-black/80 backdrop-blur-md text-gold text-[9px] tracking-[0.25em] uppercase px-3 py-1.5 font-sans font-semibold">
                <?= htmlspecialchars($artCat) ?>
              </span>
            </div>
            
            <div class="p-8 flex flex-col justify-between flex-1">
              <div>
                <p class="text-[10px] text-gray-400 font-sans tracking-widest mb-3 uppercase"><?= htmlspecialchars($artDate) ?></p>
                <h3 class="font-serif text-2xl font-light text-black mb-4 line-clamp-2 group-hover:text-gold transition-colors leading-snug">
                  <?= htmlspecialchars($art['title']) ?>
                </h3>
                <p class="text-xs text-gray-500 font-sans font-light leading-relaxed line-clamp-3 mb-6">
                  <?= htmlspecialchars($art['excerpt']) ?>
                </p>
              </div>

              <div class="pt-4 border-t border-gray-200/60 flex items-center justify-between">
                <span class="text-[10px] tracking-[0.2em] uppercase text-black font-sans font-medium group-hover:text-gold transition-colors">
                  Lire l'article
                </span>
                <span class="text-gold text-sm group-hover:translate-x-1 transition-transform">→</span>
              </div>
            </div>
          </a>
        </div>
      <?php endforeach; ?>
    </div>

  </div>
</section>

<?php include __DIR__ . '/../components/footer.php'; ?>
<script src="<?= SITE_URL ?>/assets/js/app.js"></script>
</body>
</html>
