@extends('layouts.nova')

@section('content')

{{-- ── Hero header ── --}}
@php
    $headerBreadcrumbs = collect([
        (object) ['name' => 'Creators', 'url' => '/creators/top'],
        (object) ['name' => 'Top Creators', 'url' => route('creators.top')],
    ]);
@endphp

<x-nova-page-header
    section="Creators"
    title="Top Creators"
    icon="fa-star"
    :breadcrumbs="$headerBreadcrumbs"
    :description="'Most popular creators ranked by artwork ' . ($metric === 'downloads' ? 'downloads' : 'views') . '.'"
    actionsClass="lg:pt-8"
>
    <x-slot name="actions">
        <a href="{{ route('creators.rising') }}"
           class="inline-flex items-center gap-2 rounded-lg px-4 py-2 text-sm font-medium border border-white/[0.08] bg-white/[0.04] text-white/70 hover:bg-white/[0.08] hover:text-white transition-colors">
            <i class="fa-solid fa-arrow-trend-up text-xs"></i>
            Rising Creators
        </a>

        <nav class="flex flex-wrap items-center gap-2" aria-label="Ranking metric">
            <a href="{{ request()->fullUrlWithQuery(['metric' => 'views']) }}"
               class="inline-flex items-center gap-1.5 rounded-full px-4 py-1.5 text-xs font-medium border transition-colors
                      {{ $metric === 'views' ? 'bg-sky-500/15 text-sky-300 border-sky-500/30' : 'border-white/[0.08] bg-white/[0.04] text-white/55 hover:text-white hover:bg-white/[0.08]' }}">
                <svg class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
                    <path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
                </svg>
                Views
            </a>
            <a href="{{ request()->fullUrlWithQuery(['metric' => 'downloads']) }}"
               class="inline-flex items-center gap-1.5 rounded-full px-4 py-1.5 text-xs font-medium border transition-colors
                      {{ $metric === 'downloads' ? 'bg-emerald-500/15 text-emerald-300 border-emerald-500/30' : 'border-white/[0.08] bg-white/[0.04] text-white/55 hover:text-white hover:bg-white/[0.08]' }}">
                <svg class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                </svg>
                Downloads
            </a>
        </nav>
    </x-slot>
</x-nova-page-header>

{{-- ── Leaderboard ── --}}
<div class="px-6 pt-8 pb-16 md:px-10">
    @php
        $offset = ($authors->currentPage() - 1) * $authors->perPage();
        $isFirstPage = $authors->currentPage() === 1;
        $showcaseTop = $isFirstPage ? $authors->getCollection()->take(3)->values() : collect();
        $tableAuthors = $isFirstPage ? $authors->getCollection()->slice(3)->values() : $authors->getCollection();
        $rankBase = $isFirstPage ? 3 : 0;
    @endphp

    @if ($authors->isNotEmpty())
        @if ($showcaseTop->isNotEmpty())
            <div class="mb-6 grid gap-4 md:grid-cols-3">
                @foreach ($showcaseTop as $i => $author)
                    @php
                        $rank = $i + 1;
                        $profileUrl = ($author->username ?? null)
                            ? '/@' . $author->username
                            : '/profile/' . (int) $author->user_id;
                        $avatarUrl = \App\Support\AvatarUrl::forUser((int) $author->user_id, $author->avatar_hash ?? null, 64);

                        $rankClasses = $rank === 1
                            ? 'bg-amber-400/15 text-amber-300 ring-amber-400/30'
                            : ($rank === 2
                                ? 'bg-slate-400/15 text-slate-300 ring-slate-400/30'
                                : 'bg-orange-700/20 text-orange-400 ring-orange-600/30');
                    @endphp

                    <a href="{{ $profileUrl }}"
                       class="rounded-xl border border-white/[0.08] bg-white/[0.03] p-5 hover:bg-white/[0.05] transition-colors">
                        <div class="flex items-center justify-between mb-4">
                            <span class="inline-flex items-center justify-center w-8 h-8 rounded-full text-sm font-bold ring-1 {{ $rankClasses }}">
                                {{ $rank }}
                            </span>
                            <span class="text-xs font-semibold uppercase tracking-widest {{ $metric === 'downloads' ? 'text-emerald-300/80' : 'text-sky-300/80' }}">
                                {{ $metric === 'downloads' ? 'Downloads' : 'Views' }}
                            </span>
                        </div>

                        <div class="flex items-center justify-between gap-4">
                            <img src="{{ $avatarUrl }}" alt="{{ $author->uname }}"
                                 class="w-14 h-14 rounded-full object-cover ring-1 ring-white/[0.12]">
                            <div class="min-w-0 flex-1">
                                <div class="truncate text-base font-semibold text-white">{{ $author->uname ?? 'Unknown' }}</div>
                                @if (!empty($author->username))
                                    <div class="truncate text-xs text-white/40">{{ '@' . $author->username }}</div>
                                @endif
                            </div>
                            <div class="shrink-0 text-right">
                                <div class="text-lg font-bold {{ $metric === 'downloads' ? 'text-emerald-400' : 'text-sky-400' }}">
                                    {{ number_format($author->total ?? 0) }}
                                </div>
                            </div>
                        </div>
                    </a>
                @endforeach
            </div>
        @endif

        <div class="rounded-xl border border-white/[0.06] overflow-hidden">

            {{-- Table header --}}
            <div class="grid grid-cols-[3rem_1fr_auto] items-center gap-4 px-5 py-3 bg-white/[0.03] border-b border-white/[0.06]">
                <span class="text-xs font-semibold uppercase tracking-widest text-white/30 text-center">#</span>
                <span class="text-xs font-semibold uppercase tracking-widest text-white/30">Author</span>
                <span class="text-xs font-semibold uppercase tracking-widest text-white/30 text-right">
                    {{ $metric === 'downloads' ? 'Downloads' : 'Views' }}
                </span>
            </div>

            {{-- Rows --}}
            <div class="divide-y divide-white/[0.04]">
                @foreach ($tableAuthors as $i => $author)
                    @php
                        $rank = $offset + $rankBase + $i + 1;
                        $profileUrl = ($author->username ?? null)
                            ? '/@' . $author->username
                            : '/profile/' . (int) $author->user_id;
                        $avatarUrl = \App\Support\AvatarUrl::forUser((int) $author->user_id, $author->avatar_hash ?? null, 64);
                    @endphp
                    <div class="grid grid-cols-[3rem_1fr_auto] items-center gap-4 px-5 py-4
                                {{ $rank <= 3 ? 'bg-white/[0.015]' : '' }} hover:bg-white/[0.03] transition-colors">

                        {{-- Rank badge --}}
                        <div class="text-center">
                            @if ($rank === 1)
                                <span class="inline-flex items-center justify-center w-7 h-7 rounded-full bg-amber-400/15 text-amber-300 text-xs font-bold ring-1 ring-amber-400/30">1</span>
                            @elseif ($rank === 2)
                                <span class="inline-flex items-center justify-center w-7 h-7 rounded-full bg-slate-400/15 text-slate-300 text-xs font-bold ring-1 ring-slate-400/30">2</span>
                            @elseif ($rank === 3)
                                <span class="inline-flex items-center justify-center w-7 h-7 rounded-full bg-orange-700/20 text-orange-400 text-xs font-bold ring-1 ring-orange-600/30">3</span>
                            @else
                                <span class="text-sm text-white/30 font-medium">{{ $rank }}</span>
                            @endif
                        </div>

                        {{-- Author info --}}
                        <a href="{{ $profileUrl }}" class="flex items-center gap-3 min-w-0 hover:opacity-90 transition-opacity">
                            <img src="{{ $avatarUrl }}" alt="{{ $author->uname }}"
                                 class="w-9 h-9 rounded-full object-cover flex-shrink-0 ring-1 ring-white/[0.08]">
                            <div class="min-w-0">
                                <div class="truncate text-sm font-semibold text-white/90">{{ $author->uname ?? 'Unknown' }}</div>
                                @if (!empty($author->username))
                                    <div class="truncate text-xs text-white/35">{{ '@' . $author->username }}</div>
                                @endif
                            </div>
                        </a>

                        {{-- Metric count --}}
                        <div class="text-right flex-shrink-0">
                            <span class="text-sm font-semibold {{ $metric === 'downloads' ? 'text-emerald-400' : 'text-sky-400' }}">
                                {{ number_format($author->total ?? 0) }}
                            </span>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>

        <div class="mt-8 flex justify-center">
            {{ $authors->withQueryString()->links() }}
        </div>
    @else
        <div class="rounded-xl border border-white/[0.06] bg-white/[0.02] px-8 py-12 text-center">
            <p class="text-white/40 text-sm">No authors found.</p>
        </div>
    @endif
</div>

@endsection
