<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'includes/config.php';
require_once 'includes/database.php';
require_once 'includes/class/Content.php';

$db = getDB();
$contentObj = new Content($db);
$contents = $contentObj->getAllPublished(5000);

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<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
echo '<url>
    <loc>' . SITE_URL . '</loc>
    <lastmod>' . date('Y-m-d') . '</lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
</url>';

foreach ($contents as $content) {
    echo '<url>
        <loc>' . SITE_URL . '/' . $content['slug'] . '</loc>
        <lastmod>' . date('Y-m-d', strtotime($content['updated_at'])) . '</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>';
    
    if ($content['thumbnail']) {
        echo '<image:image>
            <image:loc>' . SITE_URL . $content['thumbnail'] . '</image:loc>
            <image:title>' . htmlspecialchars($content['title']) . '</image:title>
        </image:image>';
    }
    
    echo '</url>';
}

echo '</urlset>';
?>