<?php
require_once __DIR__ . '/includes/config.php';
header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$pages = [
    ['url' => '/', 'priority' => '1.0', 'freq' => 'weekly'],
    ['url' => '/about.php', 'priority' => '0.8', 'freq' => 'monthly'],
    ['url' => '/products.php', 'priority' => '0.9', 'freq' => 'weekly'],
    ['url' => '/industries.php', 'priority' => '0.8', 'freq' => 'monthly'],
    ['url' => '/certifications.php', 'priority' => '0.7', 'freq' => 'monthly'],
    ['url' => '/factory.php', 'priority' => '0.7', 'freq' => 'monthly'],
    ['url' => '/contact.php', 'priority' => '0.8', 'freq' => 'monthly'],
    ['url' => '/quote.php', 'priority' => '0.9', 'freq' => 'monthly'],
    ['url' => '/blog.php', 'priority' => '0.7', 'freq' => 'weekly'],
    ['url' => '/dealers.php', 'priority' => '0.6', 'freq' => 'monthly'],
];

foreach ($pages as $p) {
    echo '<url>';
    echo '<loc>' . SITE_URL . $p['url'] . '</loc>';
    echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
    echo '<changefreq>' . $p['freq'] . '</changefreq>';
    echo '<priority>' . $p['priority'] . '</priority>';
    echo '</url>';
}

// Dynamic product pages
try {
    $products = db()->query("SELECT slug, updated_at FROM products WHERE is_active = 1 ORDER BY updated_at DESC LIMIT 500")->fetchAll();
    foreach ($products as $prod) {
        echo '<url>';
        echo '<loc>' . SITE_URL . '/product.php?slug=' . $prod['slug'] . '</loc>';
        echo '<lastmod>' . date('Y-m-d', strtotime($prod['updated_at'])) . '</lastmod>';
        echo '<changefreq>monthly</changefreq>';
        echo '<priority>0.7</priority>';
        echo '</url>';
    }
} catch (Exception $e) {}

// Blog posts
try {
    $posts = db()->query("SELECT slug, updated_at FROM blog_posts WHERE status = 'published' ORDER BY updated_at DESC LIMIT 100")->fetchAll();
    foreach ($posts as $post) {
        echo '<url>';
        echo '<loc>' . SITE_URL . '/blog/' . $post['slug'] . '</loc>';
        echo '<lastmod>' . date('Y-m-d', strtotime($post['updated_at'])) . '</lastmod>';
        echo '<changefreq>monthly</changefreq>';
        echo '<priority>0.6</priority>';
        echo '</url>';
    }
} catch (Exception $e) {}

echo '</urlset>';
