// sitemap.php or sitemap.xml
<?php
header('Content-Type: application/xml; charset=utf-8');

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// Get all published news
$stmt = $conn->query("
    SELECT slug, updated_at 
    FROM news 
    WHERE status = 'published' 
    ORDER BY updated_at DESC
");

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<url>';
    echo '<loc>' . getFullUrl(getNewsUrl($row['slug'])) . '</loc>';
    echo '<lastmod>' . date('Y-m-d', strtotime($row['updated_at'])) . '</lastmod>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>0.8</priority>';
    echo '</url>';
}

echo '</urlset>';
?>


<?php
require_once 'includes/config.php';
require_once 'includes/database.php';

header('Content-Type: application/xml; charset=utf-8');

// Get all published articles
$stmt = $conn->prepare("
    SELECT slug, published_at, updated_at 
    FROM news 
    WHERE status = 'published' 
    ORDER BY published_at DESC
");
$stmt->execute();
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get categories
$stmt = $conn->prepare("SELECT slug, name FROM categories WHERE status = 'active'");
$stmt->execute();
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$base_url = $protocol . $host;

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<?xml-stylesheet type="text/xsl" href="' . $base_url . '/sitemap.xsl"?>';
?>
<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">
    
    <!-- Homepage -->
    <url>
        <loc><?php echo $base_url; ?>/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Articles -->
    <?php foreach($articles as $article): ?>
    <url>
        <loc><?php echo $base_url . '/' . urlencode($article['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($article['updated_at'] ?? $article['published_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
        <news:news>
            <news:publication>
                <news:name><?php echo SITE_NAME; ?></news:name>
                <news:language>bn</news:language>
            </news:publication>
            <news:publication_date><?php echo date('Y-m-d', strtotime($article['published_at'])); ?></news:publication_date>
            <news:title><?php echo htmlspecialchars($article['title'] ?? '', ENT_QUOTES, 'UTF-8'); ?></news:title>
        </news:news>
    </url>
    <?php endforeach; ?>
    
    <!-- Categories -->
    <?php foreach($categories as $category): ?>
    <url>
        <loc><?php echo $base_url . '/category.php?slug=' . urlencode($category['slug']); ?></loc>
        <changefreq>daily</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
    
</urlset>