<?php
require_once 'includes/config.php';

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";

// Static pages
$staticPages = [
    '',
    'about',
    'services',
    'contact',
    'industries',
    'blog'
];

foreach ($staticPages as $page) {
    echo '  <url>' . "\n";
    echo '    <loc>' . BASE_URL . ($page !== '' ? '/' . $page : '') . '</loc>' . "\n";
    echo '    <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
    echo '    <changefreq>weekly</changefreq>' . "\n";
    echo '    <priority>' . ($page == '' ? '1.0' : '0.8') . '</priority>' . "\n";
    echo '  </url>' . "\n";
}

// Dynamic Services
$stmt = $pdo->query("SELECT slug FROM services WHERE status = 'active'");
$services = $stmt->fetchAll();
foreach ($services as $service) {
    echo '  <url>' . "\n";
    echo '    <loc>' . BASE_URL . '/service/' . $service['slug'] . '</loc>' . "\n";
    echo '    <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
    echo '    <changefreq>monthly</changefreq>' . "\n";
    echo '    <priority>0.9</priority>' . "\n";
    echo '  </url>' . "\n";
}

// Dynamic Blogs
$stmt = $pdo->query("SELECT slug, date FROM blogs WHERE status = 'active'");
$blogs = $stmt->fetchAll();
foreach ($blogs as $blog) {
    echo '  <url>' . "\n";
    echo '    <loc>' . BASE_URL . '/blog/' . $blog['slug'] . '</loc>' . "\n";
    echo '    <lastmod>' . date('Y-m-d', strtotime($blog['date'])) . '</lastmod>' . "\n";
    echo '    <changefreq>monthly</changefreq>' . "\n";
    echo '    <priority>0.7</priority>' . "\n";
    echo '  </url>' . "\n";
}

echo '</urlset>';
?>
