<?php
date_default_timezone_set('Asia/Kolkata');
header('Content-Type: application/xml; charset=utf-8');

define('DB_HOST', '127.0.0.1:3306');
define('DB_NAME', 'u375151508_developer_xclr');
define('DB_USER', 'u375151508_ipx_iekdiwowow');
define('DB_PASS', 'Hiys7e9^&UIhdd$%^&*()&*(');

function getDbConnection() {
    try {
        $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4";
        $pdo = new PDO($dsn, DB_USER, DB_PASS);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdo;
    } catch (PDOException $e) {
        return null;
    }
}

function escapeXml($str) {
    return htmlspecialchars($str ?? '', ENT_QUOTES, 'UTF-8');
}

function formatDateForSitemap($dateStr) {
    if (!$dateStr) return date('Y-m-d\TH:i:sP');
    try {
        $date = new DateTime($dateStr);
        $date->setTimezone(new DateTimeZone('Asia/Kolkata'));
        return $date->format('Y-m-d\TH:i:sP');
    } catch (Exception $e) {
        return date('Y-m-d\TH:i:sP');
    }
}

$pdo = getDbConnection();
$worldNews = [];

if ($pdo) {
    $stmt = $pdo->query("SELECT id, slug, title, published_at, dateModified, excerpt, featured_image, region, category 
                         FROM world_news 
                         WHERE status = 'published' 
                         ORDER BY published_at DESC");
    $worldNews = $stmt->fetchAll();
}

$siteUrl = "https://developer.khogendrarupini.com";

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
    
    <!-- World News Listing Page -->
    <url>
        <loc><?php echo $siteUrl; ?>/world/geopolitics/</loc>
        <lastmod><?php echo date('Y-m-d\TH:i:sP'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <?php foreach ($worldNews as $news): ?>
    <url>
        <loc><?php echo $siteUrl; ?>/world/geopolitics/<?php echo urlencode($news['slug']); ?></loc>
        <lastmod><?php echo formatDateForSitemap($news['dateModified'] ?? $news['published_at']); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
        
        <?php if (!empty($news['featured_image'])): ?>
        <image:image>
            <image:loc><?php echo escapeXml($news['featured_image']); ?></image:loc>
            <image:title><?php echo escapeXml($news['title']); ?></image:title>
            <image:caption><?php echo escapeXml(substr(strip_tags($news['excerpt']), 0, 200)); ?></image:caption>
        </image:image>
        <?php endif; ?>
        
        <news:news>
            <news:publication>
                <news:name>Khogendra Rupini</news:name>
                <news:language>en</news:language>
            </news:publication>
            <news:publication_date><?php echo formatDateForSitemap($news['published_at']); ?></news:publication_date>
            <news:title><?php echo escapeXml($news['title']); ?></news:title>
            <news:keywords><?php echo escapeXml($news['region'] . ', ' . $news['category'] . ', world news, geopolitics'); ?></news:keywords>
            <news:genres>PressRelease, Blog, OpEd</news:genres>
        </news:news>
    </url>
    <?php endforeach; ?>
    
</urlset>