<?php
// sitemap.php - Save this file in your root directory
// Access it via: https://yourdomain.com/sitemap.xml

header('Content-Type: application/xml; charset=utf-8');
require_once 'db.php';

$siteUrl = 'https://' . $_SERVER['HTTP_HOST'];

echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;

// ================= HOMEPAGE =================
echo '  <url>' . PHP_EOL;
echo '    <loc>' . $siteUrl . '</loc>' . PHP_EOL;
echo '    <lastmod>' . date('Y-m-d') . '</lastmod>' . PHP_EOL;
echo '    <changefreq>daily</changefreq>' . PHP_EOL;
echo '    <priority>1.0</priority>' . PHP_EOL;
echo '  </url>' . PHP_EOL;

// ================= PRODUCTS (HIGHEST PRIORITY) =================
try {
    $stmt = $pdo->query("SELECT id, slug, name, updated_at FROM products WHERE slug != '' ORDER BY updated_at DESC");
    while ($product = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $url = !empty($product['slug']) ? '/product/' . $product['slug'] : '/product.php?id=' . $product['id'];
        echo '  <url>' . PHP_EOL;
        echo '    <loc>' . $siteUrl . $url . '</loc>' . PHP_EOL;
        echo '    <lastmod>' . date('Y-m-d', strtotime($product['updated_at'])) . '</lastmod>' . PHP_EOL;
        echo '    <changefreq>weekly</changefreq>' . PHP_EOL;
        echo '    <priority>0.9</priority>' . PHP_EOL;
        echo '  </url>' . PHP_EOL;
    }
} catch (PDOException $e) {
    error_log('Sitemap product error: ' . $e->getMessage());
}

// ================= CATEGORIES =================
try {
    $stmt = $pdo->query("SELECT DISTINCT category FROM products WHERE category IS NOT NULL AND category != '' ORDER BY category");
    while ($category = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo '  <url>' . PHP_EOL;
        echo '    <loc>' . $siteUrl . '/category/' . htmlspecialchars(urlencode($category['category'])) . '</loc>' . PHP_EOL;
        echo '    <lastmod>' . date('Y-m-d') . '</lastmod>' . PHP_EOL;
        echo '    <changefreq>weekly</changefreq>' . PHP_EOL;
        echo '    <priority>0.8</priority>' . PHP_EOL;
        echo '  </url>' . PHP_EOL;
    }
} catch (PDOException $e) {
    error_log('Sitemap category error: ' . $e->getMessage());
}

// ================= STATIC PAGES =================
$staticPages = [
    '/index' => ['changefreq' => 'daily', 'priority' => '1.0'],
    '/cart' => ['changefreq' => 'daily', 'priority' => '0.8'],
    '/add_product' => ['changefreq' => 'weekly', 'priority' => '0.7'],
    '/user/login' => ['changefreq' => 'weekly', 'priority' => '0.6'],
    '/user/register' => ['changefreq' => 'weekly', 'priority' => '0.6'],
    '/user/dashboard' => ['changefreq' => 'weekly', 'priority' => '0.5'],
    '/admin/dashboard' => ['changefreq' => 'weekly', 'priority' => '0.4'],
    '/templates/support-policy' => ['changefreq' => 'monthly', 'priority' => '0.3'],
];

foreach ($staticPages as $page => $attrs) {
    echo '  <url>' . PHP_EOL;
    echo '    <loc>' . $siteUrl . $page . '</loc>' . PHP_EOL;
    echo '    <lastmod>' . date('Y-m-d') . '</lastmod>' . PHP_EOL;
    echo '    <changefreq>' . $attrs['changefreq'] . '</changefreq>' . PHP_EOL;
    echo '    <priority>' . $attrs['priority'] . '</priority>' . PHP_EOL;
    echo '  </url>' . PHP_EOL;
}

echo '</urlset>';
?>