<?php
/**
 * TheFinBaba - Dynamic Sitemap (XML)
 * Auto-includes homepage, courses, tools, standard pages, and all blog posts.
 * Apache rewrites /sitemap.xml to this script (see .htaccess).
 */
require_once __DIR__ . '/includes/config.php';

header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex');

$today = date('Y-m-d');

// Static URLs
$urls = [
  ['loc' => SITE_URL . '/',                                  'lastmod' => $today, 'changefreq' => 'weekly',  'priority' => '1.0'],
  ['loc' => SITE_URL . '/courses',                           'lastmod' => $today, 'changefreq' => 'weekly',  'priority' => '0.9'],
  ['loc' => SITE_URL . '/algo-trading-python-2025',          'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.9'],
  ['loc' => SITE_URL . '/futures-options-derivatives-2025',  'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.9'],
  ['loc' => SITE_URL . '/sock-market-basic-2025',            'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.9'],
  ['loc' => SITE_URL . '/vultr-server-for-algo-trading',     'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.9'],
  ['loc' => SITE_URL . '/static-ip-for-trading',             'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.9'],
  ['loc' => SITE_URL . '/trading-tools',                     'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.8'],
  ['loc' => SITE_URL . '/blog/',                             'lastmod' => $today, 'changefreq' => 'weekly',  'priority' => '0.8'],
  ['loc' => SITE_URL . '/about-us',                          'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.7'],
  ['loc' => SITE_URL . '/contact-us',                        'lastmod' => $today, 'changefreq' => 'yearly',  'priority' => '0.6'],
  ['loc' => SITE_URL . '/faq',                               'lastmod' => $today, 'changefreq' => 'monthly', 'priority' => '0.6'],
  ['loc' => SITE_URL . '/privacy-policy',                    'lastmod' => $today, 'changefreq' => 'yearly',  'priority' => '0.3'],
  ['loc' => SITE_URL . '/terms-condition',                   'lastmod' => $today, 'changefreq' => 'yearly',  'priority' => '0.3'],
  ['loc' => SITE_URL . '/disclaimer',                        'lastmod' => $today, 'changefreq' => 'yearly',  'priority' => '0.3'],
];

// Blog posts (with admin overrides)
if (file_exists(__DIR__ . '/blog/post-loader.php')) {
    require_once __DIR__ . '/blog/post-loader.php';
    $BLOG_POSTS = fb_load_posts();
    if (!empty($BLOG_POSTS) && is_array($BLOG_POSTS)) {
        foreach ($BLOG_POSTS as $slug => $p) {
            if (!empty($p['draft'])) continue;
            $urls[] = [
                'loc'       => SITE_URL . '/blog/' . $slug,
                'lastmod'   => $p['updated'] ?? $p['date'] ?? $today,
                'changefreq'=> 'monthly',
                'priority'  => '0.7',
            ];
        }
    }
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<?php foreach ($urls as $url): ?>
  <url>
    <loc><?= htmlspecialchars($url['loc'], ENT_XML1) ?></loc>
    <lastmod><?= $url['lastmod'] ?></lastmod>
    <changefreq><?= $url['changefreq'] ?></changefreq>
    <priority><?= $url['priority'] ?></priority>
  </url>
<?php endforeach; ?>
</urlset>
