{!! '<?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">
@foreach($products as $product)
    @php
        $imageUrls = collect();
        if ($product->featured_image) {
            $imageUrls->push([
                'loc'   => url('uploads/products/' . $product->featured_image),
                'title' => $product->featured_image_alt ?: $product->title,
            ]);
        }
        foreach ($product->images as $img) {
            if (!$img->image) {
                continue;
            }
            $loc = url('uploads/products/' . $img->image);
            if ($imageUrls->contains(fn ($row) => $row['loc'] === $loc)) {
                continue;
            }
            $imageUrls->push([
                'loc'   => $loc,
                'title' => $img->alt ?: $product->title,
            ]);
        }
        $imageUrls = $imageUrls->take(10);
    @endphp
    <url>
        <loc>{{ route('product.show', $product->slug) }}</loc>
        <lastmod>{{ $product->updated_at->toAtomString() }}</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
        @foreach($imageUrls as $image)
        <image:image>
            <image:loc>{{ $image['loc'] }}</image:loc>
            <image:title>{{ $image['title'] }}</image:title>
        </image:image>
        @endforeach
    </url>
@endforeach
</urlset>
