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

$blogsFile = __DIR__ . '/api/blogs.json';
$baseUrl = 'https://adokt.com';

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

// Homepage
echo '<url>' . "\n";
echo '<loc>' . $baseUrl . '/</loc>' . "\n";
echo '<changefreq>daily</changefreq>' . "\n";
echo '<priority>1.0</priority>' . "\n";
echo '</url>' . "\n";

// Blog listing
echo '<url>' . "\n";
echo '<loc>' . $baseUrl . '/blog/</loc>' . "\n";
echo '<changefreq>daily</changefreq>' . "\n";
echo '<priority>0.9</priority>' . "\n";
echo '</url>' . "\n";

// Main pages
$pages = ['about', 'services', 'work', 'contact', 'agencies'];
foreach ($pages as $page) {
    echo '<url>' . "\n";
    echo '<loc>' . $baseUrl . '/pages/' . $page . '.html</loc>' . "\n";
    echo '<changefreq>weekly</changefreq>' . "\n";
    echo '<priority>0.8</priority>' . "\n";
    echo '</url>' . "\n";
}

// Get blog posts
$blogs = [];
if (file_exists($blogsFile)) {
    $blogs = json_decode(file_get_contents($blogsFile), true) ?: [];
}

foreach ($blogs as $blog) {
    if ($blog['status'] === 'published') {
        echo '<url>' . "\n";
        echo '<loc>' . $baseUrl . '/blog/' . $blog['slug'] . '/</loc>' . "\n";
        echo '<lastmod>' . date('c', strtotime($blog['date'])) . '</lastmod>' . "\n";
        echo '<changefreq>monthly</changefreq>' . "\n";
        echo '<priority>0.7</priority>' . "\n";
        echo '</url>' . "\n";
    }
}

echo '</urlset>';