<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

header("Content-Type: application/xml; charset=UTF-8");
include_once __DIR__ . '/includes/config.php';

$base = "https://insideusa.site";

// Start XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// ------------------ POSTS ------------------
$stmt = $pdo->query("SELECT slug FROM posts");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<url>';
     echo '<loc>' . $base . '/post.php?slug=' . urlencode(trim($row['slug'])) . '</loc>';
    echo '<changefreq>hourly</changefreq>';
    echo '<priority>0.9</priority>';
    echo '</url>';
}

// ------------------ PAGES ------------------
$pages = [
    "index.php"     => "daily",
    "about.php"     => "monthly",
    "contact.php"   => "monthly",
    "privacy.php"   => "yearly",
    "terms.php"     => "yearly",
    "search.php"    => "daily"
];

foreach ($pages as $page => $freq) {
    echo '<url>';
    echo '<loc>' . $base . '/' . $page . '</loc>';
    echo '<changefreq>' . $freq . '</changefreq>';
    echo '<priority>0.8</priority>';
    echo '</url>';
}

// ------------------ CATEGORIES ------------------
$stmt = $pdo->query("SELECT slug FROM categories");
while ($cat = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<url>';
    echo '<loc>' . $base . '/category/' . htmlspecialchars($cat['slug'], ENT_XML1) . '</loc>';
    echo '<changefreq>daily</changefreq>';
    echo '<priority>0.7</priority>';
    echo '</url>';
}

// End XML
echo '</urlset>';
exit;
