<?php
declare(strict_types=1);
require_once __DIR__ . '/../app/config/bootstrap.php';
require_once __DIR__ . '/../app/models/Product.php';

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

$base = rtrim(APP_URL, '/');
$urls = [];
$urls[] = ['loc' => $base . appPath('/public/home.php'), 'changefreq' => 'daily', 'priority' => '1.0'];
$urls[] = ['loc' => $base . appPath('/public/products.php'), 'changefreq' => 'daily', 'priority' => '0.9'];

try {
    foreach ((new Product())->all(500) as $p) {
        if (empty($p['slug'])) {
            continue;
        }
        $urls[] = [
            'loc' => $base . appPath('/public/product.php') . '?slug=' . rawurlencode((string)$p['slug']),
            'changefreq' => 'weekly',
            'priority' => '0.8',
        ];
    }
} catch (Throwable $e) {
}

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