<?php
/**
 * Project: AID-DZ.SITE - Absolute SEO Dynamic Sitemap V10.0
 * Dev: Serge (The Architect)
 * Update: المزامنة الكاملة مع نسخة V10 والـ Slugs الديناميكية
 */

include 'db.php';

// دالة الـ Slug لضمان التطابق مع ملف الـ Index (في حال لم تكن معرفة في db.php)
if (!function_exists('create_slug')) {
    function create_slug($string) {
        return preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
    }
}

header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

  <url>
    <loc>https://aid-dz.site/</loc>
    <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    <changefreq>hourly</changefreq>
    <priority>1.00</priority>
  </url>

  <url>
    <loc>https://aid-dz.site/asaar-essouk-el-youm.php</loc>
    <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    <changefreq>hourly</changefreq>
    <priority>1.00</priority>
  </url>

  <url>
    <loc>https://aid-dz.site/donate.php</loc>
    <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.90</priority>
  </url>

  <?php
  // المقالات السيادية الثابتة (توقعات الأسعار ودليل النقاط)
  $static_articles = [
      'توقعات أسعار الكباش لعام 2026 في الجزائر: تقرير استراتيجي شامل',
      'دليل نقاط بيع الأضاحي الرسمية 2026: مبادرة المنتج للمستهلك'
  ];

  foreach ($static_articles as $title) {
      $slug = create_slug($title);
      echo "  <url>
    <loc>https://aid-dz.site/index.php?art_title=" . $slug . "</loc>
    <lastmod>" . date('Y-m-d') . "</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.95</priority>
  </url>\n";
  }

  // جلب المقالات الديناميكية من قاعدة البيانات
  try {
      if (isset($pdo)) {
          $stmt = $pdo->query("SELECT title, posted_at FROM blog_posts ORDER BY id DESC LIMIT 250");
          while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
              $slug = create_slug($row['title']); 
              $lastmod = (!empty($row['posted_at'])) ? date('Y-m-d', strtotime($row['posted_at'])) : date('Y-m-d');
              
              echo "  <url>
    <loc>https://aid-dz.site/index.php?art_title=" . $slug . "</loc>
    <lastmod>{$lastmod}</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.80</priority>
  </url>\n";
          }
      }
  } catch (Exception $e) { }
  ?>

  <url>
    <loc>https://aid-dz.site/about.php</loc>
    <lastmod>2026-05-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.50</priority>
  </url>

  <url>
    <loc>https://aid-dz.site/privacy.php</loc>
    <lastmod>2026-05-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.30</priority>
  </url>

  <url>
    <loc>https://aid-dz.site/contact.php</loc>
    <lastmod>2026-05-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.50</priority>
  </url>

</urlset>
