<?php
// sitemap.xml - يتم توليدها تلقائياً
header('Content-Type: application/xml; charset=utf-8');

require_once 'includes/config.php';

$db = Database::getInstance();

// جلب جميع الروابط
$urls = [];

// الصفحات الرئيسية
$pages = [
    '',
    'about.php',
    'projects.php',
    'news.php',
    'gallery.php',
    'initiatives.php',
    'success-stories.php',
    'contact.php',
    'team.php',
    'partners.php'
];

foreach ($pages as $page) {
    $urls[] = [
        'loc' => BASE_URL . $page,
        'priority' => $page == '' ? '1.0' : '0.8',
        'changefreq' => 'daily'
    ];
}

// المشاريع
$projects = $db->fetchAll("SELECT id, created_at FROM projects WHERE status = 'active'");
foreach ($projects as $project) {
    $urls[] = [
        'loc' => BASE_URL . 'project-detail.php?id=' . $project['id'],
        'priority' => '0.7',
        'changefreq' => 'weekly',
        'lastmod' => date('Y-m-d', strtotime($project['created_at']))
    ];
}

// الأخبار
$news = $db->fetchAll("SELECT id, published_at FROM news WHERE status = 'published'");
foreach ($news as $item) {
    $urls[] = [
        'loc' => BASE_URL . 'news-detail.php?id=' . $item['id'],
        'priority' => '0.6',
        'changefreq' => 'weekly',
        'lastmod' => date('Y-m-d', strtotime($item['published_at']))
    ];
}

// قصص النجاح
$stories = $db->fetchAll("SELECT id, created_at FROM success_stories WHERE status = 'published'");
foreach ($stories as $story) {
    $urls[] = [
        'loc' => BASE_URL . 'story-detail.php?id=' . $story['id'],
        'priority' => '0.6',
        'changefreq' => 'monthly',
        'lastmod' => date('Y-m-d', strtotime($story['created_at']))
    ];
}

// المبادرات
$initiatives = $db->fetchAll("SELECT id, created_at FROM initiatives WHERE status != 'completed'");
foreach ($initiatives as $initiative) {
    $urls[] = [
        'loc' => BASE_URL . 'initiative-detail.php?id=' . $initiative['id'],
        'priority' => '0.5',
        'changefreq' => 'monthly',
        'lastmod' => date('Y-m-d', strtotime($initiative['created_at']))
    ];
}

// الألبومات
$albums = $db->fetchAll("SELECT id, created_at FROM albums WHERE status = 'active'");
foreach ($albums as $album) {
    $urls[] = [
        'loc' => BASE_URL . 'album-detail.php?id=' . $album['id'],
        'priority' => '0.5',
        'changefreq' => 'monthly',
        'lastmod' => date('Y-m-d', strtotime($album['created_at']))
    ];
}

// الصفحات المخصصة
$pages = $db->fetchAll("SELECT slug, created_at FROM pages WHERE status = 'published'");
foreach ($pages as $page) {
    $urls[] = [
        'loc' => BASE_URL . 'page.php?slug=' . $page['slug'],
        'priority' => '0.7',
        'changefreq' => 'weekly',
        'lastmod' => date('Y-m-d', strtotime($page['created_at']))
    ];
}

// إنشاء XML
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

foreach ($urls as $url) {
    echo "\t<url>\n";
    echo "\t\t<loc>" . htmlspecialchars($url['loc']) . "</loc>\n";
    echo "\t\t<priority>" . $url['priority'] . "</priority>\n";
    echo "\t\t<changefreq>" . $url['changefreq'] . "</changefreq>\n";
    if (isset($url['lastmod'])) {
        echo "\t\t<lastmod>" . $url['lastmod'] . "</lastmod>\n";
    }
    echo "\t</url>\n";
}

echo '</urlset>';