<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    
    <!-- Homepage -->
    <url>
        <loc>https://www.vibecart.lk/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Cart Page -->
    <url>
        <loc>https://www.vibecart.lk/cart.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <!-- Categories -->
    <?php
    require_once 'admin/db-config.php';
    $categories = fetchAll("SELECT * FROM categories WHERE active = 1");
    foreach ($categories as $category):
    ?>
    <url>
        <loc>https://www.vibecart.lk/category.php?category=<?php echo urlencode($category['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($category['updated_at'] ?? $category['created_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
        <?php if (!empty($category['image'])): ?>
        <image:image>
            <image:loc>https://www.vibecart.lk/<?php echo htmlspecialchars($category['image']); ?></image:loc>
            <image:title><?php echo htmlspecialchars($category['name']); ?></image:title>
        </image:image>
        <?php endif; ?>
    </url>
    <?php endforeach; ?>
    
    <!-- Products -->
    <?php
    $products = fetchAll("SELECT * FROM products WHERE active = 1");
    foreach ($products as $product):
    ?>
    <url>
        <loc>https://www.vibecart.lk/product-detail.php?id=<?php echo $product['id']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($product['updated_at'] ?? $product['created_at'])); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
        <?php if (!empty($product['image'])): ?>
        <image:image>
            <image:loc>https://www.vibecart.lk/<?php echo htmlspecialchars($product['image']); ?></image:loc>
            <image:title><?php echo htmlspecialchars($product['name']); ?></image:title>
            <image:caption><?php echo htmlspecialchars($product['description'] ?? $product['name']); ?></image:caption>
        </image:image>
        <?php endif; ?>
    </url>
    <?php endforeach; ?>
    
</urlset>
