<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
            <div>
                <p class="text-sm font-bold uppercase tracking-wide text-blue-600 dark:text-blue-300">Admin Avatars</p>
                <h2 class="text-2xl font-black text-slate-950 dark:text-white">Avatar Management</h2>
            </div>
            <a href="{{ route('admin.avatars.create') }}" class="inline-flex rounded-lg bg-blue-600 px-5 py-2 text-sm font-black text-white hover:bg-blue-700">
                Upload Avatar
            </a>
        </div>
    </x-slot>

    <div class="rounded-lg border border-blue-100 bg-white shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-blue-100 dark:divide-blue-900/50">
                <thead class="bg-blue-50/70 dark:bg-blue-950/30">
                    <tr>
                        <th class="px-5 py-3 text-left text-xs font-black uppercase tracking-wide text-slate-500 dark:text-slate-300">Avatar</th>
                        <th class="px-5 py-3 text-left text-xs font-black uppercase tracking-wide text-slate-500 dark:text-slate-300">Status</th>
                        <th class="px-5 py-3 text-left text-xs font-black uppercase tracking-wide text-slate-500 dark:text-slate-300">Default</th>
                        <th class="px-5 py-3 text-right text-xs font-black uppercase tracking-wide text-slate-500 dark:text-slate-300">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-100 dark:divide-blue-900/50">
                    @forelse ($avatars as $avatar)
                        <tr>
                            <td class="px-5 py-4">
                                <div class="flex items-center gap-3">
                                    <img src="{{ $avatar->imageUrl() }}" alt="{{ $avatar->name }}" class="h-12 w-12 rounded-full object-cover ring-2 ring-blue-100 dark:ring-blue-900">
                                    <div>
                                        <p class="font-black text-slate-950 dark:text-white">{{ $avatar->name }}</p>
                                        <p class="text-xs text-slate-500 dark:text-slate-400">{{ $avatar->image_path }}</p>
                                    </div>
                                </div>
                            </td>
                            <td class="px-5 py-4">
                                <span class="rounded-full px-3 py-1 text-xs font-black {{ $avatar->status === 'active' ? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-950/50 dark:text-emerald-200' : 'bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-300' }}">
                                    {{ ucfirst($avatar->status) }}
                                </span>
                            </td>
                            <td class="px-5 py-4 text-sm font-bold text-slate-600 dark:text-slate-300">
                                {{ $avatar->is_default ? 'Yes' : 'No' }}
                            </td>
                            <td class="px-5 py-4">
                                <div class="flex flex-wrap justify-end gap-2">
                                    <a href="{{ route('admin.avatars.edit', $avatar) }}" class="rounded-lg border border-blue-100 px-3 py-2 text-xs font-black text-blue-700 hover:bg-blue-50 dark:border-blue-900 dark:text-blue-300 dark:hover:bg-blue-950/60">Edit</a>

                                    <form method="POST" action="{{ route('admin.avatars.toggle-status', $avatar) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="rounded-lg border border-blue-100 px-3 py-2 text-xs font-black text-blue-700 hover:bg-blue-50 dark:border-blue-900 dark:text-blue-300 dark:hover:bg-blue-950/60">
                                            {{ $avatar->status === 'active' ? 'Deactivate' : 'Activate' }}
                                        </button>
                                    </form>

                                    @unless ($avatar->is_default)
                                        <form method="POST" action="{{ route('admin.avatars.set-default', $avatar) }}">
                                            @csrf
                                            @method('PATCH')
                                            <button class="rounded-lg border border-blue-100 px-3 py-2 text-xs font-black text-blue-700 hover:bg-blue-50 dark:border-blue-900 dark:text-blue-300 dark:hover:bg-blue-950/60">Set Default</button>
                                        </form>

                                        <form method="POST" action="{{ route('admin.avatars.delete', $avatar) }}" onsubmit="return confirm('Delete this avatar? Users using it will return to the default avatar.');">
                                            @csrf
                                            @method('DELETE')
                                            <button class="rounded-lg border border-red-200 px-3 py-2 text-xs font-black text-red-700 hover:bg-red-50 dark:border-red-900/60 dark:text-red-300 dark:hover:bg-red-950/40">Delete</button>
                                        </form>
                                    @endunless
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="px-5 py-10 text-center text-sm font-bold text-slate-500 dark:text-slate-400">No avatars found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="border-t border-blue-100 px-5 py-4 dark:border-blue-900/50">
            {{ $avatars->links() }}
        </div>
    </div>
</x-app-layout>
