<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'config/database.php';

$urls = [
    ['loc' => 'https://dhaniwin.app/', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => 'https://dhaniwin.app/dhaniwin-login', 'priority' => '0.9', 'changefreq' => 'weekly'],
    ['loc' => 'https://dhaniwin.app/dhaniwin-register', 'priority' => '0.9', 'changefreq' => 'weekly'],
    ['loc' => 'https://dhaniwin.app/dhaniwin-app-download', 'priority' => '0.9', 'changefreq' => 'weekly'],
    ['loc' => 'https://dhaniwin.app/dhanigame', 'priority' => '0.9', 'changefreq' => 'weekly'],
    ['loc' => 'https://dhaniwin.app/about', 'priority' => '0.7', 'changefreq' => 'monthly'],
    ['loc' => 'https://dhaniwin.app/contact', 'priority' => '0.7', 'changefreq' => 'monthly'],
    ['loc' => 'https://dhaniwin.app/privacy-policy', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'https://dhaniwin.app/disclaimer', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'https://dhaniwin.app/blog', 'priority' => '0.8', 'changefreq' => 'weekly']
];

$result = $conn->query("SELECT slug, updated_at FROM posts WHERE status = 'published'");
if($result) {
    while($row = $result->fetch_assoc()) {
        $urls[] = [
            'loc' => 'https://dhaniwin.app/blog/' . $row['slug'],
            'priority' => '0.8',
            'changefreq' => 'weekly'
        ];
    }
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($urls as $url) {
    echo '<url>';
    echo '<loc>' . htmlspecialchars($url['loc']) . '</loc>';
    echo '<priority>' . $url['priority'] . '</priority>';
    echo '<changefreq>' . $url['changefreq'] . '</changefreq>';
    echo '</url>';
}
echo '</urlset>';
?>