<?php
header('Content-type: application/xml');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

$base_url = "https://primevertexsphere.com";

$static_pages = [
    '/', '/about-us.php', '/contact-us.php', '/service.php',
    '/web-development.php', '/app-development.php', '/ai-automation.php', '/saas-development.php',
    '/ui-ux-design.php', '/graphic-design.php', '/digital-marketing.php', '/domain-hosting.php',
    '/pack.php', '/portfolio.php', '/client-feedback.php', '/blog.php', '/faq.php'
];

$date = date("Y-m-d");

// Add static pages
foreach ($static_pages as $page) {
    $priority = ($page == '/') ? '1.0' : '0.8';
    $url = $base_url . $page;
    // Replace .php manually for cleaner URLs
    $clean_url = str_replace('.php', '', $url);
    
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($clean_url) . "</loc>\n";
    echo "    <lastmod>{$date}</lastmod>\n";
    echo "    <priority>{$priority}</priority>\n";
    echo "  </url>\n";
}

// Add dynamic blog pages
include('blogs_data.php');
if (isset($blogs) && is_array($blogs)) {
    foreach ($blogs as $blog) {
        if (!empty($blog['slug'])) {
            // Make sure slug is URL safe
            $slug = htmlspecialchars($blog['slug']);
            $blog_url = $base_url . "/blog/" . $slug;
            
            echo "  <url>\n";
            echo "    <loc>{$blog_url}</loc>\n";
            echo "    <lastmod>{$date}</lastmod>\n";
            echo "    <priority>0.7</priority>\n";
            echo "  </url>\n";
        }
    }
}

// Add programmatic SEO pages from cached cities and countries
$countries = ['pakistan', 'usa', 'uk', 'canada', 'australia', 'uae'];
$cities_list = [];
$city_files = glob(__DIR__ . '/cache/cities/*.json');
if (is_array($city_files)) {
    foreach ($city_files as $f) {
        $cities_list[] = basename($f, '.json');
    }
}
$locations = array_merge($countries, $cities_list);

$pseo_keywords = [
    'best-software-agency',
    'number-1-software-house',
    'top-web-development-company',
    'best-digital-marketing-agency',
    'number-1-flutter-developer',
    'best-software-development-company',
    'top-web-development-agency',
    'affordable-web-development-agency',
    'best-ai-automation-agency',
    'top-saas-development-company'
];

foreach ($locations as $loc) {
    foreach ($pseo_keywords as $kw) {
        $loc_url = $base_url . "/" . $kw . "-in-" . $loc;
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($loc_url) . "</loc>\n";
        echo "    <lastmod>{$date}</lastmod>\n";
        echo "    <priority>0.6</priority>\n";
        echo "  </url>\n";
    }
}

echo '</urlset>';
?>

