<?php
require_once __DIR__ . '/includes/functions.php';

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

$urls = [];

// Static pages
$static = [
    ['loc' => '', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => 'customer/request-quote.php', 'priority' => '0.9', 'changefreq' => 'weekly'],
    ['loc' => 'auth/login.php', 'priority' => '0.3', 'changefreq' => 'monthly'],
    ['loc' => 'auth/register.php', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'contact.php', 'priority' => '0.4', 'changefreq' => 'monthly'],
    ['loc' => 'blog.php', 'priority' => '0.6', 'changefreq' => 'weekly'],
];

// Dynamic pages from DB
$pages = $pdo->query("SELECT slug, updated_at FROM pages WHERE published = 1")->fetchAll();
foreach ($pages as $p) {
    $static[] = [
        'loc' => 'page.php?slug=' . $p['slug'],
        'priority' => '0.5',
        'changefreq' => 'monthly',
        'lastmod' => $p['updated_at']
    ];
}

// Categories
$cats = $pdo->query("SELECT slug FROM categories")->fetchAll();
foreach ($cats as $c) {
    $static[] = [
        'loc' => 'index.php?category=' . $c['slug'] . '#categories',
        'priority' => '0.7',
        'changefreq' => 'weekly'
    ];
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($static as $s): ?>
  <url>
    <loc><?= SITE_URL ?>/<?= $s['loc'] ?></loc>
    <priority><?= $s['priority'] ?></priority>
    <changefreq><?= $s['changefreq'] ?></changefreq>
    <?php if (isset($s['lastmod'])): ?>
    <lastmod><?= date('Y-m-d', strtotime($s['lastmod'])) ?></lastmod>
    <?php endif; ?>
  </url>
<?php endforeach; ?>
</urlset>
