<?php
/**
 * sitemap.xml - ملف XML لخريطة الموقع لمحركات البحث
 */

header('Content-Type: application/xml; charset=utf-8');

require_once 'config/config.php';

try {
    $db = db();
} catch (Exception $e) {
    // إذا فشل الاتصال، أخرج XML أساسي
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    echo '<url><loc>' . SITE_URL . '/</loc><priority>1.0</priority></url>';
    echo '</urlset>';
    exit;
}

// بدء XML
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// الصفحات الرئيسية
$main_pages = [
    ['url' => '', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['url' => 'about.php', 'priority' => '0.8', 'changefreq' => 'monthly'],
    ['url' => 'all-candidates.php', 'priority' => '0.9', 'changefreq' => 'daily'],
    ['url' => 'nominate.php', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['url' => 'contact.php', 'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => 'login.php', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['url' => 'register.php', 'priority' => '0.6', 'changefreq' => 'monthly'],
    ['url' => 'social/index.php', 'priority' => '0.9', 'changefreq' => 'daily'],
    ['url' => 'sitemap.php', 'priority' => '0.6', 'changefreq' => 'weekly']
];

foreach ($main_pages as $page) {
    $xml .= '<url>';
    $xml .= '<loc>' . SITE_URL . '/' . $page['url'] . '</loc>';
    $xml .= '<priority>' . $page['priority'] . '</priority>';
    $xml .= '<changefreq>' . $page['changefreq'] . '</changefreq>';
    $xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>';
    $xml .= '</url>';
}

// المنشورات
try {
    $posts = $db->query("SELECT id, updated_at FROM posts WHERE status = 'published' ORDER BY id DESC LIMIT 500");
    foreach ($posts as $post) {
        $xml .= '<url>';
        $xml .= '<loc>' . SITE_URL . '/social/post.php?id=' . $post['id'] . '</loc>';
        $xml .= '<priority>0.7</priority>';
        $xml .= '<changefreq>weekly</changefreq>';
        $xml .= '<lastmod>' . date('Y-m-d', strtotime($post['updated_at'])) . '</lastmod>';
        $xml .= '</url>';
    }
} catch (Exception $e) {}

// المرشحين
try {
    $candidates = $db->query("SELECT id, updated_at FROM candidates WHERE status = 'approved' ORDER BY id DESC LIMIT 500");
    foreach ($candidates as $candidate) {
        $xml .= '<url>';
        $xml .= '<loc>' . SITE_URL . '/candidate.php?id=' . $candidate['id'] . '</loc>';
        $xml .= '<priority>0.8</priority>';
        $xml .= '<changefreq>weekly</changefreq>';
        $xml .= '<lastmod>' . date('Y-m-d', strtotime($candidate['updated_at'] ?? 'now')) . '</lastmod>';
        $xml .= '</url>';
    }
} catch (Exception $e) {}

// المستخدمين
try {
    $users = $db->query("SELECT id, updated_at FROM users WHERE user_type = 'user' ORDER BY id DESC LIMIT 500");
    foreach ($users as $user) {
        $xml .= '<url>';
        $xml .= '<loc>' . SITE_URL . '/social/profile.php?id=' . $user['id'] . '</loc>';
        $xml .= '<priority>0.6</priority>';
        $xml .= '<changefreq>weekly</changefreq>';
        $xml .= '<lastmod>' . date('Y-m-d', strtotime($user['updated_at'])) . '</lastmod>';
        $xml .= '</url>';
    }
} catch (Exception $e) {}

$xml .= '</urlset>';

echo $xml;
?>