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

// Include API configuration
require_once 'config/api_config.php';

// Fetch all blog posts dynamically from API (fetch all pages)
$dynamicUrls = [];
$page = 1;
do {
    $blogResponse = makeApiRequest('api/blog/blogs/' . $page . '/1', 'GET');
    $blogs = [];
    $totalPages = 1;
    if ($blogResponse && $blogResponse['status'] == 200 && isset($blogResponse['data']['blogs'])) {
        $blogs = $blogResponse['data']['blogs'];
        $totalBlogs = $blogResponse['data']['total_blog_count'] ?? 0;
        $totalPages = (int)ceil($totalBlogs / 9);
        foreach ($blogs as $blog) {
            if (isset($blog['post_slug'])) {
                $dynamicUrls[] = [
                    'url' => 'https://nashtechpeb.com/' . $blog['post_slug'],
                    'lastmod' => isset($blog['updated_at']) ? date('Y-m-d', strtotime($blog['updated_at'])) : date('Y-m-d'),
                    'changefreq' => 'weekly',
                    'priority' => '0.8'
                ];
            }
        }
    }
    $page++;
} while ($page <= $totalPages);

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

// Static URLs (core pages)
$staticUrls = [
    ['loc' => 'https://nashtechpeb.com/', 'lastmod' => date('Y-m-d'), 'changefreq' => 'weekly', 'priority' => '1.0'],
    ['loc' => 'https://nashtechpeb.com/capabilities', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/certificates-recognition', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => 'https://nashtechpeb.com/client-satisfaction', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => 'https://nashtechpeb.com/commercial-steel-building-construction', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/company-overview', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/contact-nashtech', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.9'],
    ['loc' => 'https://nashtechpeb.com/customised-steel-structure', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/executive-team', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['loc' => 'https://nashtechpeb.com/gallery', 'lastmod' => date('Y-m-d'), 'changefreq' => 'weekly', 'priority' => '0.7'],
    ['loc' => 'https://nashtechpeb.com/light-gauge-framing-system', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/mezzanine-floor-construction', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/nhai-steel-bridge-fabrication', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/our-process', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => 'https://nashtechpeb.com/peb-manufacturers', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.9'],
    ['loc' => 'https://nashtechpeb.com/projects', 'lastmod' => date('Y-m-d'), 'changefreq' => 'weekly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/railway-steel-bridge-fabrication', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/roofing-and-cladding', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/why-nashtech', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/blog', 'lastmod' => date('Y-m-d'), 'changefreq' => 'weekly', 'priority' => '0.8'],
    ['loc' => 'https://nashtechpeb.com/privacy-policy', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.5'],
    ['loc' => 'https://nashtechpeb.com/terms-and-conditions', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.5'],
    // Project Pages
    ['loc' => 'https://nashtechpeb.com/sandeep-enterprises-peb-industrial-building-reengus-sikar', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => 'https://nashtechpeb.com/agarwal-glass-peb-factory-building-sitapura-jaipur', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => 'https://nashtechpeb.com/3s-minerals-peb-factory-building-udaipur', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => 'https://nashtechpeb.com/yogi-stones-peb-warehouse-building-bagru', 'lastmod' => date('Y-m-d'), 'changefreq' => 'monthly', 'priority' => '0.7']
];

// Output static URLs
foreach ($staticUrls as $url) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($url['loc']) . "</loc>\n";
    echo "    <lastmod>" . htmlspecialchars($url['lastmod']) . "</lastmod>\n";
    echo "    <changefreq>" . htmlspecialchars($url['changefreq']) . "</changefreq>\n";
    echo "    <priority>" . htmlspecialchars($url['priority']) . "</priority>\n";
    echo "  </url>\n";
}

// Output dynamic blog URLs from API
foreach ($dynamicUrls as $url) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($url['url']) . "</loc>\n";
    echo "    <lastmod>" . htmlspecialchars($url['lastmod']) . "</lastmod>\n";
    echo "    <changefreq>" . htmlspecialchars($url['changefreq']) . "</changefreq>\n";
    echo "    <priority>" . htmlspecialchars($url['priority']) . "</priority>\n";
    echo "  </url>\n";
}

echo '</urlset>' . "\n";
?>