@extends('dashboard.app')

@section('page_title', 'Admin Users')
@section('page_subtitle', 'Manage accounts, roles, and access tiers.')

@section('dashboard_content')
    @if (session('status'))
        <div class="mb-6 rounded-2xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-700 dark:border-emerald-500/30 dark:bg-emerald-500/15 dark:text-emerald-200">
            {{ session('status') }}
        </div>
    @endif
    @if ($errors->any())
        <div class="mb-6 rounded-2xl border border-amber-300/40 bg-amber-400/10 px-4 py-3 text-sm text-amber-200">
            {{ $errors->first() }}
        </div>
    @endif

    <div class="grid gap-6 lg:grid-cols-3">
        @foreach ([
            ['label' => 'Total users', 'value' => number_format(\App\Models\User::count()), 'note' => 'All accounts'],
            ['label' => 'Personal tier', 'value' => number_format(\App\Models\User::where('tier', 'personal')->count()), 'note' => 'Active subscriptions'],
            ['label' => 'Admins', 'value' => number_format(\App\Models\User::where('role', 'admin')->count()), 'note' => 'Staff'],
        ] as $card)
            <div class="rounded-2xl glass-card p-5">
                <div class="text-xs uppercase tracking-[0.2em] text-gray-400">{{ $card['label'] }}</div>
                <div class="mt-3 text-3xl font-semibold text-white">{{ $card['value'] }}</div>
                <div class="mt-2 text-sm text-gray-400">{{ $card['note'] }}</div>
            </div>
        @endforeach
    </div>

    <div class="mt-8 rounded-2xl glass-card p-6">
        <div class="mb-6">
            <div class="flex flex-wrap items-center justify-between gap-3">
                <div>
                    <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Create</div>
                    <div class="mt-2 text-lg font-semibold text-white">Add new user</div>
                </div>
                <button id="toggle-create-user" type="button" class="rounded-full border border-white/10 px-4 py-2 text-xs font-semibold text-gray-200 hover:bg-white/5">
                    Show form
                </button>
            </div>
            <form id="create-user-form" method="POST" action="{{ route('dashboard.admin.users.create') }}" class="mt-4 grid gap-3 md:grid-cols-2 hidden">
                @csrf
                <div>
                    <label class="text-xs uppercase tracking-[0.15em] text-gray-500">Name</label>
                    <input type="text" name="name" placeholder="Optional" class="mt-2 w-full rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-brand-ocean/40 theme-surface">
                </div>
                <div>
                    <label class="text-xs uppercase tracking-[0.15em] text-gray-500">Email</label>
                    <input type="email" name="email" required class="mt-2 w-full rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-brand-ocean/40 theme-surface">
                </div>
                <div>
                    <label class="text-xs uppercase tracking-[0.15em] text-gray-500">Password</label>
                    <input type="password" name="password" minlength="8" required class="mt-2 w-full rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-brand-ocean/40 theme-surface">
                </div>
                <div>
                    <label class="text-xs uppercase tracking-[0.15em] text-gray-500">Role</label>
                    <select name="role" class="mt-2 w-full rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 theme-surface">
                        <option value="user">User</option>
                        <option value="admin">Admin</option>
                    </select>
                </div>
                <div>
                    <label class="text-xs uppercase tracking-[0.15em] text-gray-500">Tier</label>
                    <select name="tier" class="mt-2 w-full rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 theme-surface">
                        <option value="free">Free</option>
                        <option value="personal">Personal</option>
                    </select>
                </div>
                <div class="flex items-end">
                    <button class="w-full rounded-xl bg-brand-ocean text-white font-semibold px-4 py-2 text-sm">Create user</button>
                </div>
            </form>
        </div>

        <div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
            <div>
                <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Directory</div>
                <div class="mt-2 text-lg font-semibold text-white">User list</div>
            </div>
            <form method="GET" class="flex w-full flex-wrap gap-3 md:w-auto md:justify-end">
                <input type="hidden" name="sort" value="{{ $sort ?? 'id' }}">
                <input type="hidden" name="dir" value="{{ $dir ?? 'desc' }}">
                <input type="search" name="q" value="{{ $filters['q'] ?? '' }}" placeholder="Search by email or name"
                       class="w-full md:w-72 rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-brand-ocean/40 theme-surface">
                <select name="tier" class="rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 theme-surface">
                    <option value="">All tiers</option>
                    <option value="free" @selected(($filters['tier'] ?? '') === 'free')>Free</option>
                    <option value="personal" @selected(($filters['tier'] ?? '') === 'personal')>Personal</option>
                </select>
                <select name="role" class="rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 theme-surface">
                    <option value="">All roles</option>
                    <option value="admin" @selected(($filters['role'] ?? '') === 'admin')>Admin</option>
                    <option value="user" @selected(($filters['role'] ?? '') === 'user')>User</option>
                </select>
                <button class="rounded-xl border border-white/10 px-4 py-2 text-sm font-semibold text-gray-200 hover:bg-white/5 transition-colors">Filter</button>
            </form>
        </div>

        <div class="mt-6 overflow-x-auto">
            <table class="min-w-full text-left text-sm">
                <thead class="text-xs uppercase tracking-[0.15em] text-gray-400">
                    <tr>
                        @php
                            $sortParam = $sort ?? 'id';
                            $dirParam = $dir ?? 'desc';
                            $toggle = fn ($field) => ($sortParam === $field && $dirParam === 'asc') ? 'desc' : 'asc';
                            $sortUrl = fn ($field) => request()->fullUrlWithQuery(['sort' => $field, 'dir' => $toggle($field)]);
                        @endphp
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('name') }}" class="inline-flex items-center gap-1 hover:text-white">
                                User
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('role') }}" class="inline-flex items-center gap-1 hover:text-white">
                                Role
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('tier') }}" class="inline-flex items-center gap-1 hover:text-white">
                                Tier
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4">Status</th>
                        <th class="py-3 pr-4">
                            <a href="{{ $sortUrl('created_at') }}" class="inline-flex items-center gap-1 hover:text-white">
                                Joined
                                <i data-lucide="arrow-up-down" class="w-3 h-3"></i>
                            </a>
                        </th>
                        <th class="py-3 pr-4 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/10 text-gray-300">
                    @forelse ($users ?? [] as $user)
                        <tr>
                            <td class="py-4 pr-4">
                                <div class="font-semibold text-white">{{ $user->name ?? '—' }}</div>
                                <div class="text-xs text-gray-400">{{ $user->email }}</div>
                            </td>
                            <td class="py-4 pr-4">{{ $user->role ?? 'user' }}</td>
                            <td class="py-4 pr-4">
                                <form method="POST" action="{{ route('dashboard.admin.users.tier') }}" class="flex items-center gap-2">
                                    @csrf
                                    <input type="hidden" name="user_id" value="{{ $user->id }}">
                                    <select name="tier" class="rounded-full border border-white/10 px-3 py-1 text-xs font-semibold text-gray-200 theme-surface">
                                        <option value="free" @selected($user->tier === 'free')>Free</option>
                                        <option value="personal" @selected($user->tier === 'personal')>Personal</option>
                                    </select>
                                    <button class="text-xs font-semibold text-brand-ocean hover:text-brand-oceanSoft">Save</button>
                                </form>
                            </td>
                            <td class="py-4 pr-4">
                                <span class="rounded-full bg-emerald-100 px-3 py-1 text-xs font-semibold text-emerald-800 dark:bg-emerald-500/20 dark:text-emerald-200">Active</span>
                            </td>
                            <td class="py-4 pr-4 text-xs">{{ $user->created_at?->toDateString() }}</td>
                            <td class="py-4 pr-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{{ route('dashboard.admin.users.show', $user->id) }}" class="rounded-full border border-white/10 px-3 py-1 text-xs font-semibold text-gray-200 hover:bg-white/5 transition-colors">View</a>
                                    <form method="POST" action="{{ route('dashboard.admin.users.delete', $user->id) }}" data-confirm="Delete this user? This removes their data and cannot be undone." data-confirm-title="Delete user" data-confirm-ok="Delete">
                                        @csrf
                                        @method('DELETE')
                                        <button class="rounded-full border border-rose-200 px-3 py-1 text-xs font-semibold text-rose-700 hover:bg-rose-50 transition-colors dark:border-rose-500/40 dark:text-rose-200 dark:hover:bg-rose-500/10">Delete</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="py-6 text-center text-sm text-gray-400">No users found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="mt-6">
            {{ $users->links('vendor.pagination.dashboard') }}
        </div>
    </div>
@endsection

@push('scripts')
<script>
(() => {
    const toggleBtn = document.getElementById('toggle-create-user');
    const form = document.getElementById('create-user-form');
    if (!toggleBtn || !form) return;

    const setState = (open) => {
        form.classList.toggle('hidden', !open);
        toggleBtn.textContent = open ? 'Hide form' : 'Show form';
    };

    const hasErrors = @json($errors->any());
    setState(hasErrors);

    toggleBtn.addEventListener('click', () => {
        setState(form.classList.contains('hidden'));
    });
})();
</script>
@endpush
