<?php
/**
 * XML Sitemap Generator for Vaarahi Self Drive Cars
 */
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/includes/seo_functions.php';

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

$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');

$pdo = getDBConnection();

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

    <!-- Static Pages -->
    <url>
        <loc><?php echo $baseUrl; ?>/index.php</loc>
        <priority>1.0</priority>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/cars.php</loc>
        <priority>0.9</priority>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/about.php</loc>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/contact.php</loc>
        <priority>0.6</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/faq.php</loc>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/list-your-car.php</loc>
        <priority>0.6</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/finance.php</loc>
        <priority>0.5</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/insurance.php</loc>
        <priority>0.5</priority>
        <changefreq>monthly</changefreq>
    </url>

    <!-- Location Landing Pages -->
    <url>
        <loc><?php echo $baseUrl; ?>/kukatpally.php</loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/miyapur.php</loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/gachibowli.php</loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/hitech-city.php</loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/kondapur.php</loc>
        <priority>0.7</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/madhapur.php</loc>
        <priority>0.7</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/jubilee-hills.php</loc>
        <priority>0.7</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/secunderabad.php</loc>
        <priority>0.7</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/begumpet.php</loc>
        <priority>0.7</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?php echo $baseUrl; ?>/ameerpet.php</loc>
        <priority>0.7</priority>
        <changefreq>weekly</changefreq>
    </url>

<?php
// Dynamic car detail pages
try {
    $stmt = $pdo->query("SELECT slug, updated_at FROM cars WHERE status IN ('available','booked') AND is_verified = 1 ORDER BY updated_at DESC");
    while ($car = $stmt->fetch()) {
        $updated = $car['updated_at'] ? date('Y-m-d\TH:i:sP', strtotime($car['updated_at'])) : date('Y-m-d\TH:i:sP');
        ?>
    <url>
        <loc><?php echo $baseUrl; ?>/car-details.php?slug=<?php echo urlencode($car['slug']); ?></loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
        <lastmod><?php echo $updated; ?></lastmod>
    </url>
        <?php
    }
} catch (Exception $e) {
    // Silently fail
}
?>

</urlset>