<?php
// sitemap.php
// Dynamically generates a sitemap.xml file.

// IMPORTANT: Replace this with your actual website's base URL
$base_url = "https://www.autorym.ro";

// Load the car data helper
require_once __DIR__ . '/data_helpers.php';

// Set the content type to XML
header('Content-Type: application/xml; charset=utf-8');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// 1. Home Page
echo "  <url>\n";
echo "    <loc>" . htmlspecialchars($base_url . '/index.php') . "</loc>\n";
echo "    <priority>1.0</priority>\n";
echo "    <changefreq>daily</changefreq>\n";
echo "  </url>\n";

// 2. AI Assistant Page
echo "  <url>\n";
echo "    <loc>" . htmlspecialchars($base_url . '/ai.php') . "</loc>\n"; // Corrected from ai.html
echo "    <priority>0.7</priority>\n";
echo "  </url>\n";

// 3. Privacy Policy Page
echo "  <url>\n";
echo "    <loc>" . htmlspecialchars($base_url . '/privacy.php') . "</loc>\n"; // Added
echo "    <priority>0.3</priority>\n";
echo "    <changefreq>monthly</changefreq>\n";
echo "  </url>\n";

// 4. All Car Detail Pages
$cars = get_all_cars();
if (!empty($cars)) {
    foreach ($cars as $car) {
        if (isset($car['id'])) {
            echo "  <url>\n";
            echo "    <loc>" . htmlspecialchars($base_url . '/car_detail.php?id=' . $car['id']) . "</loc>\n";
            echo "    <priority>0.8</priority>\n";
            // Use 'created' date if available, otherwise just today
            $lastmod = date('Y-m-d', strtotime($car['created'] ?? 'now'));
            echo "    <lastmod>" . $lastmod . "</lastmod>\n";
            echo "  </url>\n";
        }
    }
}

echo '</urlset>';
?>