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

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

// Base URL
$base_url = SITE_URL;

// Homepage
echo '<url>';
echo '<loc>' . htmlspecialchars($base_url) . '</loc>';
echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
echo '<changefreq>daily</changefreq>';
echo '<priority>1.0</priority>';
echo '</url>';

// Static pages
$static_pages = [
    'products.php' => ['priority' => '0.9', 'changefreq' => 'daily'],
    'about-us.php' => ['priority' => '0.8', 'changefreq' => 'monthly'],
    'contact.php' => ['priority' => '0.7', 'changefreq' => 'monthly'],
    'blog.php' => ['priority' => '0.8', 'changefreq' => 'weekly'],
    'faq.php' => ['priority' => '0.7', 'changefreq' => 'monthly'],
    'privacy.php' => ['priority' => '0.5', 'changefreq' => 'yearly'],
    'terms.php' => ['priority' => '0.5', 'changefreq' => 'yearly'],
    'shipping-policy.php' => ['priority' => '0.6', 'changefreq' => 'monthly'],
    'refund-policy.php' => ['priority' => '0.6', 'changefreq' => 'monthly'],
];

foreach ($static_pages as $page => $seo) {
    echo '<url>';
    echo '<loc>' . htmlspecialchars($base_url . '/' . $page) . '</loc>';
    echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
    echo '<changefreq>' . $seo['changefreq'] . '</changefreq>';
    echo '<priority>' . $seo['priority'] . '</priority>';
    echo '</url>';
}

// Products
$products_query = "SELECT slug, updated_at FROM products WHERE is_active = 1 ORDER BY updated_at DESC LIMIT 1000";
$products_result = mysqli_query($conn, $products_query);
if ($products_result) {
    while ($product = mysqli_fetch_assoc($products_result)) {
        echo '<url>';
        echo '<loc>' . htmlspecialchars($base_url . '/product-details.php?slug=' . $product['slug']) . '</loc>';
        echo '<lastmod>' . date('Y-m-d', strtotime($product['updated_at'])) . '</lastmod>';
        echo '<changefreq>weekly</changefreq>';
        echo '<priority>0.8</priority>';
        echo '</url>';
    }
}

// Categories
$categories_query = "SELECT slug, updated_at FROM categories WHERE is_active = 1 ORDER BY updated_at DESC";
$categories_result = mysqli_query($conn, $categories_query);
if ($categories_result) {
    while ($category = mysqli_fetch_assoc($categories_result)) {
        echo '<url>';
        echo '<loc>' . htmlspecialchars($base_url . '/products.php?category=' . $category['slug']) . '</loc>';
        echo '<lastmod>' . date('Y-m-d', strtotime($category['updated_at'])) . '</lastmod>';
        echo '<changefreq>weekly</changefreq>';
        echo '<priority>0.7</priority>';
        echo '</url>';
    }
}

// Blog posts
$blogs_query = "SELECT slug, updated_at FROM blogs WHERE is_published = 1 ORDER BY updated_at DESC";
$blogs_result = mysqli_query($conn, $blogs_query);
if ($blogs_result) {
    while ($blog = mysqli_fetch_assoc($blogs_result)) {
        echo '<url>';
        echo '<loc>' . htmlspecialchars($base_url . '/blog-details.php?slug=' . $blog['slug']) . '</loc>';
        echo '<lastmod>' . date('Y-m-d', strtotime($blog['updated_at'])) . '</lastmod>';
        echo '<changefreq>weekly</changefreq>';
        echo '<priority>0.7</priority>';
        echo '</url>';
    }
}

echo '</urlset>';
?>
