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

$base = 'https://unlocksubs.com';
$today = date('Y-m-d');

$static_pages = [
    ['loc' => '/',                                          'priority' => '1.0', 'changefreq' => 'daily',   'lastmod' => $today],
    ['loc' => '/blog/',                                     'priority' => '0.9', 'changefreq' => 'weekly',  'lastmod' => $today],
    ['loc' => '/blog/sub2unlock-kya-hai',                   'priority' => '0.9', 'changefreq' => 'monthly', 'lastmod' => '2025-08-01'],
    ['loc' => '/blog/sub2unlock-se-paise-kaise-kamaye',     'priority' => '0.9', 'changefreq' => 'monthly', 'lastmod' => '2025-08-01'],
    ['loc' => '/blog/link-locker-kya-hota-hai',             'priority' => '0.8', 'changefreq' => 'monthly', 'lastmod' => '2025-08-01'],
    ['loc' => '/blog/best-link-locker-india-2025',          'priority' => '0.8', 'changefreq' => 'monthly', 'lastmod' => '2025-08-01'],
    ['loc' => '/blog/youtube-subscribers-kaise-badhaye',    'priority' => '0.8', 'changefreq' => 'monthly', 'lastmod' => '2025-08-01'],
    ['loc' => '/register.php',                              'priority' => '0.8', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['loc' => '/login.php',                                 'priority' => '0.6', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['loc' => '/pages/contact.php',                         'priority' => '0.5', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['loc' => '/pages/privacy.php',                         'priority' => '0.4', 'changefreq' => 'yearly',  'lastmod' => $today],
    ['loc' => '/pages/terms.php',                           'priority' => '0.4', 'changefreq' => 'yearly',  'lastmod' => $today],
];

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">

<?php foreach ($static_pages as $p): ?>
    <url>
        <loc><?= htmlspecialchars($base . $p['loc']); ?></loc>
        <lastmod><?= $p['lastmod']; ?></lastmod>
        <changefreq><?= $p['changefreq']; ?></changefreq>
        <priority><?= $p['priority']; ?></priority>
    </url>
<?php endforeach; ?>

<?php
// Dynamic unlock pages (public links only)
$linkRes = $conn->query("SELECT unique_code, link_title, updated_at, thumbnail FROM links WHERE is_active = 1 ORDER BY id DESC LIMIT 500");
if ($linkRes) {
    while ($link = $linkRes->fetch_assoc()) {
        $loc  = htmlspecialchars($base . '/' . $link['unique_code']);
        $mod  = date('Y-m-d', strtotime($link['updated_at'] ?? 'now'));
        $title = htmlspecialchars($link['link_title'] ?: 'Locked Content');
        echo "    <url>\n        <loc>$loc</loc>\n        <lastmod>$mod</lastmod>\n        <changefreq>monthly</changefreq>\n        <priority>0.6</priority>\n";
        if (!empty($link['thumbnail'])) {
            $img = htmlspecialchars($base . '/' . ltrim($link['thumbnail'], '/'));
            echo "        <image:image><image:loc>$img</image:loc><image:title>$title</image:title></image:image>\n";
        }
        echo "    </url>\n";
    }
}
?>
</urlset>
