<?php
/**
 * ================================================================
 * Sitemap داینامیک - انتخاب نوین
 * ================================================================
 * آدرس: sitemap.xml (با htaccess ریدایرکت کنید)
 * یا مستقیم: sitemap.php
 * ================================================================
 */

header('Content-Type: application/xml; charset=utf-8');

require_once 'config/database.php';

$siteUrl = 'https://entekhabnovin.ir';

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">

    <!-- صفحه اصلی -->
    <url>
        <loc><?php echo $siteUrl; ?>/</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>hourly</changefreq>
        <priority>1.0</priority>
        <mobile:mobile/>
    </url>

    <!-- دسته‌بندی‌ها -->
    <?php
    try {
        $categories = $pdo->query("
            SELECT DISTINCT category 
            FROM news 
            WHERE category IS NOT NULL AND category != ''
        ")->fetchAll(PDO::FETCH_COLUMN);
        
        foreach ($categories as $cat):
    ?>
    <url>
        <loc><?php echo $siteUrl; ?>/category.php?cat=<?php echo urlencode($cat); ?></loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
        <mobile:mobile/>
    </url>
    <?php endforeach; } catch (Exception $e) {} ?>

    <!-- همه اخبار -->
    <url>
        <loc><?php echo $siteUrl; ?>/category.php?cat=<?php echo urlencode('همه'); ?></loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>hourly</changefreq>
        <priority>0.9</priority>
        <mobile:mobile/>
    </url>

    <!-- خبرهای ویژه -->
    <url>
        <loc><?php echo $siteUrl; ?>/category.php?cat=<?php echo urlencode('ویژه'); ?></loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
        <mobile:mobile/>
    </url>

    <!-- اخبار -->
    <?php
    try {
        $news = $pdo->query("
            SELECT id, title, image, category, created_at 
            FROM news 
            ORDER BY created_at DESC 
            LIMIT 1000
        ")->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($news as $item):
    ?>
    <url>
        <loc><?php echo $siteUrl; ?>/single.php?id=<?php echo (int)$item['id']; ?></loc>
        <lastmod><?php echo date('c', strtotime($item['created_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
        <mobile:mobile/>
        <?php if (!empty($item['image'])): ?>
        <image:image>
            <image:loc><?php echo $siteUrl; ?>/uploads/<?php echo htmlspecialchars($item['image']); ?></image:loc>
            <image:title><?php echo htmlspecialchars($item['title']); ?></image:title>
        </image:image>
        <?php endif; ?>
        <news:news>
            <news:publication>
                <news:name>انتخاب نوین</news:name>
                <news:language>fa</news:language>
            </news:publication>
            <news:publication_date><?php echo date('c', strtotime($item['created_at'])); ?></news:publication_date>
            <news:title><?php echo htmlspecialchars($item['title']); ?></news:title>
            <news:keywords><?php echo htmlspecialchars($item['category']); ?></news:keywords>
        </news:news>
    </url>
    <?php endforeach; } catch (Exception $e) {} ?>

</urlset>