@extends('layouts.admin')
@section('title', 'Dashboard — JAM Admin')
@section('page-title', 'Dashboard')

@section('content')
<div class="mb-8">
    <h2 class="text-2xl font-semibold" style="font-family: 'Playfair Display', serif;">Welcome back, {{ auth('admin')->user()->name }}</h2>
    <p class="text-white/40 text-sm mt-1">Here's what's happening with JAM Members Club.</p>
</div>

{{-- Stats cards --}}
<div class="grid grid-cols-4 gap-5 mb-8">
    @php
        $totalMembers  = \App\Models\User::count();
        $activeMembers = \App\Models\User::where('status', 'active')->count();
        $totalPoints   = \App\Models\PointsTransaction::where('type', 'credit')->sum('amount');
        $newThisWeek   = \App\Models\User::where('created_at', '>=', now()->subDays(7))->count();
    @endphp
    <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
        <p class="text-white/40 text-xs tracking-widest uppercase mb-3">Total Members</p>
        <p class="text-3xl font-semibold text-white">{{ number_format($totalMembers) }}</p>
        <p class="text-white/30 text-xs mt-2">{{ $activeMembers }} active</p>
    </div>
    <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
        <p class="text-white/40 text-xs tracking-widest uppercase mb-3">Points Issued</p>
        <p class="text-3xl font-semibold text-[#D4B84A]">{{ number_format($totalPoints) }}</p>
        <p class="text-white/30 text-xs mt-2">All time</p>
    </div>
    <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
        <p class="text-white/40 text-xs tracking-widest uppercase mb-3">New This Week</p>
        <p class="text-3xl font-semibold text-white">{{ $newThisWeek }}</p>
        <p class="text-white/30 text-xs mt-2">Registrations</p>
    </div>
    <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
        <p class="text-white/40 text-xs tracking-widest uppercase mb-3">Tier Breakdown</p>
        @php $tiers = \App\Models\Tier::withCount(['members'])->orderBy('display_order')->get(); @endphp
        <div class="space-y-1">
            @foreach($tiers as $tier)
            <div class="flex justify-between items-center">
                <span class="text-white/50 text-xs">{{ $tier->name }}</span>
                <span class="text-white text-xs font-medium">{{ $tier->members_count }}</span>
            </div>
            @endforeach
        </div>
    </div>
</div>

{{-- Recent members --}}
<div class="bg-[#111111] border border-[#1e1e1e] rounded-xl">
    <div class="flex items-center justify-between px-6 py-4 border-b border-[#1e1e1e]">
        <h3 class="text-sm font-medium">Recent Members</h3>
        <a href="{{ route('admin.members.index') }}" class="text-[#D4B84A] text-xs hover:text-[#E8D080] transition-colors">View all →</a>
    </div>
    <div class="divide-y divide-[#1e1e1e]">
        @foreach(\App\Models\User::with('currentTier')->latest()->take(5)->get() as $user)
        <div class="flex items-center justify-between px-6 py-4">
            <div class="flex items-center gap-4">
                <div class="w-9 h-9 rounded-full bg-[#1e1e1e] flex items-center justify-center">
                    <span class="text-white/60 text-sm font-medium">{{ strtoupper(substr($user->first_name, 0, 1)) }}</span>
                </div>
                <div>
                    <p class="text-white text-sm font-medium">{{ $user->full_name }}</p>
                    <p class="text-white/40 text-xs">{{ $user->email }}</p>
                </div>
            </div>
            <div class="flex items-center gap-6">
                <div class="text-right">
                    <p class="text-white text-sm font-medium">{{ number_format($user->points_balance) }} pts</p>
                    <p class="text-white/30 text-xs">{{ $user->currentTier?->tier?->name ?? 'Invited' }}</p>
                </div>
                <span class="px-2.5 py-1 rounded-full text-xs font-medium
                    {{ $user->status === 'active' ? 'bg-green-900/40 text-green-400' : 'bg-red-900/40 text-red-400' }}">
                    {{ ucfirst($user->status) }}
                </span>
                <a href="{{ route('admin.members.show', $user) }}" class="text-white/30 hover:text-[#D4B84A] transition-colors">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 5l7 7-7 7"/></svg>
                </a>
            </div>
        </div>
        @endforeach
        @if(\App\Models\User::count() === 0)
        <div class="px-6 py-8 text-center text-white/30 text-sm">No members yet.</div>
        @endif
    </div>
</div>
@endsection
