<?php
/**
 * تذكيراتي — مولد Sitemap.xml
 */
header('Content-Type: application/xml; charset=utf-8');

$base = 'https://' . ($_SERVER['HTTP_HOST'] ?? 'reminderati.com');

// جلب الصفحات المنشورة من قاعدة البيانات إن وجدت
$pages = [];
$blog_posts = [];

if (file_exists(__DIR__ . '/config.php')) {
    require_once __DIR__ . '/config.php';
    try {
        $pages = rem_fetch_all("SELECT slug, updated_at FROM " . DB_PAGES . " WHERE is_published = 1");
        $blog_posts = rem_fetch_all("SELECT slug, updated_at FROM " . DB_BLOG . " WHERE is_published = 1");
    } catch (Exception $e) {
        // تجاهل أخطاء قاعدة البيانات
    }
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- الصفحات الرئيسية -->
    <url><loc><?= $base ?>/</loc><changefreq>weekly</changefreq><priority>1.0</priority></url>
    <url><loc><?= $base ?>/pricing</loc><changefreq>monthly</changefreq><priority>0.9</priority></url>
    <url><loc><?= $base ?>/blog</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
    <url><loc><?= $base ?>/contact</loc><changefreq>monthly</changefreq><priority>0.5</priority></url>

    <!-- صفحات ثابتة -->
    <?php foreach ($pages as $page): ?>
    <url>
        <loc><?= $base ?>/page/<?= htmlspecialchars($page['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($page['updated_at'] ?? 'now')) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>

    <!-- مقالات المدونة -->
    <?php foreach ($blog_posts as $post): ?>
    <url>
        <loc><?= $base ?>/blog/<?= htmlspecialchars($post['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($post['updated_at'] ?? 'now')) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
</urlset>
