<?php
// NewsLA.us Dynamic XML Sitemap Generator
// Generates sitemap from database content

header("Content-Type: application/xml; charset=UTF-8");

// Database connection
require_once __DIR__ . '/php-backend/config/database.php';

$database = new Database();
$db = $database->getConnection();

if (!$db) {
    die('<?xml version="1.0" encoding="UTF-8"?><urlset></urlset>');
}

// Start XML output
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">
    
    <!-- Homepage -->
    <url>
        <loc>https://newsla.us/</loc>
        <changefreq>always</changefreq>
        <priority>1.0</priority>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    </url>
    
    <!-- Category Pages -->
    <?php
    $categories = ['breaking', 'politics', 'economy', 'tech', 'opinion'];
    foreach ($categories as $category): ?>
    <url>
        <loc>https://newsla.us/<?php echo $category; ?></loc>
        <changefreq>hourly</changefreq>
        <priority>0.8</priority>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    </url>
    <?php endforeach; ?>
    
    <!-- Recent Articles (last 1000) -->
    <?php
    try {
        $query = "SELECT 
            id, 
            title, 
            url, 
            category, 
            published_date,
            source
        FROM posts 
        WHERE published_date > DATE_SUB(NOW(), INTERVAL 48 HOUR)
        ORDER BY published_date DESC 
        LIMIT 1000";
        
        $stmt = $db->prepare($query);
        $stmt->execute();
        $articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($articles as $article):
            // Create a URL-friendly slug from title
            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $article['title'])));
            $articleUrl = "https://newsla.us/article/" . $article['id'] . "/" . substr($slug, 0, 50);
        ?>
    <url>
        <loc><?php echo htmlspecialchars($articleUrl); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($article['published_date'])); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
        <news:news>
            <news:publication>
                <news:name>NewsLA.us</news:name>
                <news:language>en</news:language>
            </news:publication>
            <news:publication_date><?php echo date('c', strtotime($article['published_date'])); ?></news:publication_date>
            <news:title><?php echo htmlspecialchars($article['title']); ?></news:title>
        </news:news>
    </url>
        <?php endforeach;
    } catch (Exception $e) {
        // Silent fail - don't break sitemap
    }
    ?>
    
    <!-- Network Sites -->
    <url>
        <loc>https://videola.us/</loc>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
    <url>
        <loc>https://forumla.us/</loc>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
</urlset>
