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

    <url>
        <loc>https://sourceandshipuk.com/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        <image:image>
            <image:loc>https://sourceandshipuk.com/assets/images/Markey%20time.jpeg</image:loc>
            <image:title>Source&Ship - UK Groceries Delivered to Ghana</image:title>
        </image:image>
    </url>

    <url>
        <loc>https://sourceandshipuk.com/shop.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>

    <url>
        <loc>https://sourceandshipuk.com/about.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc>https://sourceandshipuk.com/contact.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc>https://sourceandshipuk.com/track.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>

    <?php
    require_once __DIR__ . '/includes/init.php';
    $db = Database::getInstance();
    $products = $db->fetchAll("SELECT slug, name, image, updated_at FROM products WHERE is_active = 1 ORDER BY updated_at DESC");
    foreach ($products as $prod):
        $imgUrl = !empty($prod['image']) ? 'https://sourceandshipuk.com/uploads/products/' . rawurlencode($prod['image']) : '';
    ?>
    <url>
        <loc>https://sourceandshipuk.com/product.php?slug=<?= urlencode($prod['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($prod['updated_at'])) ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
        <?php if ($imgUrl): ?>
        <image:image>
            <image:loc><?= $imgUrl ?></image:loc>
            <image:title><?= htmlspecialchars($prod['name']) ?></image:title>
        </image:image>
        <?php endif; ?>
    </url>
    <?php endforeach; ?>

    <?php
    $categories = $db->fetchAll("SELECT slug, name FROM categories WHERE is_active = 1");
    foreach ($categories as $cat):
    ?>
    <url>
        <loc>https://sourceandshipuk.com/shop.php?category=<?= urlencode($cat['slug']) ?></loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>

</urlset>
