<?php
/**
 * Sitemap生成脚本
 * 运行: php sitemap.php
 * 文件路径: /sitemap.xml
 */

$siteUrl = 'http://localhost/course-system';
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';

// 静态页面
$staticPages = [
    ['loc' => '/index.php', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => '/index.php?action=course_list', 'priority' => '0.9', 'changefreq' => 'daily'],
    ['loc' => '/index.php?action=login', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => '/index.php?action=register', 'priority' => '0.5', 'changefreq' => 'monthly'],
];

foreach ($staticPages as $page) {
    $xml .= '
    <url>
        <loc>' . $siteUrl . $page['loc'] . '</loc>
        <changefreq>' . $page['changefreq'] . '</changefreq>
        <priority>' . $page['priority'] . '</priority>
    </url>';
}

// 动态页面 - 课程
try {
    $db = new PDO('sqlite:' . __DIR__ . '/data/course.db');
    $courses = $db->query("SELECT id FROM courses WHERE status = 'published'")->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($courses as $course) {
        $xml .= '
    <url>
        <loc>' . $siteUrl . '/index.php?action=course_detail&id=' . $course['id'] . '</loc>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>';
    }
} catch (Exception $e) {
    // 数据库未创建，跳过
}

$xml .= '
</urlset>';

file_put_contents(__DIR__ . '/sitemap.xml', $xml);
echo 'Sitemap generated: ' . __DIR__ . '/sitemap.xml';