
@php
	$gridV2    = request()->query('grid') === 'v2';
	$seoPage   = (int) request()->query('page', 1);
	$seoBase   = url()->current();
	$seoCanonical = $seoPage > 1 ? $seoBase . '?page=' . $seoPage : $seoBase;
	$seoPrev   = $seoPage > 1
		? ($seoPage === 2 ? $seoBase : $seoBase . '?page=' . ($seoPage - 1))
		: null;
	$seoNext   = (isset($latestUploads) && method_exists($latestUploads, 'hasMorePages') && $latestUploads->hasMorePages())
		? $seoBase . '?page=' . ($seoPage + 1)
		: null;
	$homeUploadsItems = collect(method_exists($latestUploads ?? null, 'items') ? $latestUploads->items() : ($latestUploads ?? []));
	$homeGalleryArtworks = $homeUploadsItems->map(fn ($upload) => [
		'id' => $upload->id ?? null,
		'name' => $upload->name ?? $upload->title ?? null,
		'slug' => $upload->slug ?? \Illuminate\Support\Str::slug($upload->name ?? $upload->title ?? 'artwork'),
		'url' => $upload->url ?? ((isset($upload->id) && $upload->id) ? '/art/' . $upload->id . '/' . ($upload->slug ?? \Illuminate\Support\Str::slug($upload->name ?? $upload->title ?? 'artwork')) : '#'),
		'thumb' => $upload->thumb ?? $upload->thumb_url ?? null,
		'thumb_url' => $upload->thumb_url ?? $upload->thumb ?? null,
		'thumb_srcset' => $upload->thumb_srcset ?? null,
		'uname' => $upload->uname ?? $upload->author_name ?? '',
		'username' => $upload->username ?? $upload->uname ?? '',
		'avatar_url' => $upload->avatar_url ?? null,
		'content_type_name' => $upload->content_type_name ?? '',
		'content_type_slug' => $upload->content_type_slug ?? '',
		'category_name' => $upload->category_name ?? '',
		'category_slug' => $upload->category_slug ?? '',
		'width' => $upload->width ?? null,
		'height' => $upload->height ?? null,
		'published_at' => !empty($upload->published_at)
			? (method_exists($upload->published_at, 'toIsoString') ? $upload->published_at->toIsoString() : (string) $upload->published_at)
			: null,
	]);
	$homeGalleryNextPageUrl = method_exists($latestUploads ?? null, 'nextPageUrl') ? $latestUploads->nextPageUrl() : null;
@endphp

@push('head')
<link rel="canonical" href="{{ $seoCanonical ?? url()->current() }}">
@if(!empty($seoPrev ?? null))<link rel="prev" href="{{ $seoPrev }}">@endif
@if(!empty($seoNext ?? null))<link rel="next" href="{{ $seoNext }}">@endif
@endpush

{{-- Latest uploads grid — use same Nova gallery layout as /browse --}}
<section class="px-6 pb-10 pt-6 md:px-10">
	<div
		data-react-masonry-gallery
		data-artworks='@json($homeGalleryArtworks)'
		data-gallery-type="home-uploads"
		@if ($homeGalleryNextPageUrl) data-next-page-url="{{ $homeGalleryNextPageUrl }}" @endif
		data-limit="{{ method_exists($latestUploads ?? null, 'perPage') ? $latestUploads->perPage() : $homeGalleryArtworks->count() }}"
		class="min-h-32"
	></div>
</section>

@push('styles')
@if(! ($gridV2 ?? false))
<style>
	[data-nova-gallery].is-enhanced [data-gallery-grid] {
		display: grid;
		grid-template-columns: repeat(1, minmax(0, 1fr));
		grid-auto-rows: 8px;
		gap: 1rem;
	}
	@media (min-width: 768px) {
		[data-nova-gallery].is-enhanced [data-gallery-grid] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
	}
	@media (min-width: 1024px) {
		[data-nova-gallery].is-enhanced [data-gallery-grid] { grid-template-columns: repeat(5, minmax(0, 1fr)); }
	}
	@media (min-width: 2600px) {
		[data-nova-gallery].is-enhanced [data-gallery-grid] { grid-template-columns: repeat(6, minmax(0, 1fr)); }
	}
	[data-nova-gallery].is-enhanced [data-gallery-grid] > .nova-card { margin: 0 !important; }
	[data-nova-gallery].is-enhanced [data-gallery-pagination] { display: none; }
	[data-gallery-skeleton].is-loading { display: grid !important; grid-template-columns: inherit; gap: 1rem; }
</style>
@endif
@endpush

@push('scripts')
@vite('resources/js/entry-masonry-gallery.jsx')
@endpush

