<?php
// Start buffering immediately to trap any BOM or whitespace from includes
ob_start();

require_once __DIR__ . '/config.php';
$db = getDB();

// Discard anything that config.php might have accidentally printed
ob_end_clean();

// Now it is 100% safe to send XML headers
header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";

$base = rtrim(SITE_URL, '/');
$now = date('Y-m-d');
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
  <loc>TEST MAP 2 - <?= SITE_URL ?>/</loc>
  <xhtml:link rel="alternate" hreflang="en" href="<?= SITE_URL ?>/"/>
  <xhtml:link rel="alternate" hreflang="fr" href="<?= SITE_URL ?>/?lang=fr"/>
  <lastmod><?= $now ?></lastmod>
  <priority>1.0</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/mobile</loc>
  <xhtml:link rel="alternate" hreflang="en" href="<?= SITE_URL ?>/mobile"/>
  <xhtml:link rel="alternate" hreflang="fr" href="<?= SITE_URL ?>/mobile?lang=fr"/>
  <lastmod><?= $now ?></lastmod>
  <priority>0.95</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/internet</loc>
  <xhtml:link rel="alternate" hreflang="en" href="<?= SITE_URL ?>/internet"/>
  <xhtml:link rel="alternate" hreflang="fr" href="<?= SITE_URL ?>/internet?lang=fr"/>
  <lastmod><?= $now ?></lastmod>
  <priority>0.95</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/tv</loc>
  <xhtml:link rel="alternate" hreflang="en" href="<?= SITE_URL ?>/tv"/>
  <xhtml:link rel="alternate" hreflang="fr" href="<?= SITE_URL ?>/tv?lang=fr"/>
  <lastmod><?= $now ?></lastmod>
  <priority>0.95</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/phones</loc>
  <xhtml:link rel="alternate" hreflang="en" href="<?= SITE_URL ?>/phones"/>
  <xhtml:link rel="alternate" hreflang="fr" href="<?= SITE_URL ?>/phones?lang=fr"/>
  <lastmod><?= $now ?></lastmod>
  <priority>0.95</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/provinces</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.9</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/cities</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.9</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/compare</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.8</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/reviews</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.8</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/faq</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.8</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/about</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.5</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/contact</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.5</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/privacy</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.3</priority>
</url>
<url>
  <loc><?= SITE_URL ?>/terms</loc>
  <lastmod><?= $now ?></lastmod>
  <priority>0.3</priority>
</url>
<?php foreach($db->query("SELECT slug, updated_at FROM provinces WHERE is_active=1") as $p): ?>
<url>
  <loc><?= SITE_URL ?>/province/<?= $p['slug'] ?></loc>
  <lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod>
  <priority>0.8</priority>
</url>
<?php endforeach; ?>
<?php foreach($db->query("SELECT c.slug as cslug, p.slug as pslug, c.updated_at FROM cities c JOIN provinces p ON c.province_id=p.id WHERE c.is_active=1") as $c): ?>
<url>
  <loc><?= SITE_URL ?>/city/<?= $c['pslug'] ?>/<?= $c['cslug'] ?></loc>
  <lastmod><?= date('Y-m-d', strtotime($c['updated_at'])) ?></lastmod>
  <priority>0.7</priority>
</url>
<?php endforeach; ?>
</urlset>
