@extends('layouts.admin')
@section('title', $user->full_name . ' — JAM Admin')
@section('page-title', $user->full_name)

@section('content')

{{-- Flash messages --}}
@if(session('success'))
<div class="bg-green-900/20 border border-green-700/40 rounded-xl px-5 py-3 mb-6 text-green-400 text-sm">
    {{ session('success') }}
</div>
@endif
@if(session('error'))
<div class="bg-red-900/20 border border-red-700/40 rounded-xl px-5 py-3 mb-6 text-red-400 text-sm">
    {{ session('error') }}
</div>
@endif

<div x-data="{ suspendModal: false, debitModal: false }" class="space-y-6">

    {{-- Header --}}
    <div class="flex items-start justify-between">
        <div>
            <a href="{{ route('admin.members.index') }}" class="text-white/30 hover:text-white text-xs transition-colors inline-flex items-center gap-1 mb-3">
                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg>
                Members
            </a>
            <h2 class="text-2xl font-semibold" style="font-family: 'Playfair Display', serif;">{{ $user->full_name }}</h2>
            <p class="text-white/40 text-sm mt-1">
                {{ $user->membership_number }}
                · Joined {{ $user->enrolled_at?->format('d M Y') }}
                · {{ $user->email }}
            </p>
        </div>
        <div class="flex items-center gap-2">
            <a href="{{ route('admin.tiers.evaluate', $user) }}"
               class="border border-[#2a2a2a] hover:border-[#D4B84A]/40 text-white/40 hover:text-[#D4B84A] text-xs tracking-wide uppercase px-4 py-2.5 rounded-full transition-colors">
                Evaluate Tier
            </a>
            <a href="{{ route('admin.members.edit', $user) }}"
               class="border border-[#2a2a2a] hover:border-[#D4B84A]/50 text-white/50 hover:text-[#D4B84A] text-xs tracking-wide uppercase px-4 py-2.5 rounded-full transition-colors">
                Edit
            </a>
            @if($user->status === 'active')
            <button @click="suspendModal = true"
                    class="border border-orange-800/50 hover:border-orange-500/60 text-orange-400/60 hover:text-orange-400 text-xs tracking-wide uppercase px-4 py-2.5 rounded-full transition-colors">
                Suspend
            </button>
            <form method="POST" action="{{ route('admin.members.deactivate', $user) }}">
                @csrf
                <button type="submit" onclick="return confirm('Deactivate {{ $user->full_name }}? They will not be able to log in.')"
                        class="border border-red-800/50 hover:border-red-600 text-red-400/60 hover:text-red-400 text-xs tracking-wide uppercase px-4 py-2.5 rounded-full transition-colors">
                    Deactivate
                </button>
            </form>
            @else
            <form method="POST" action="{{ route('admin.members.reactivate', $user) }}">
                @csrf
                <button type="submit"
                        class="border border-green-800/50 hover:border-green-500 text-green-400/60 hover:text-green-400 text-xs tracking-wide uppercase px-4 py-2.5 rounded-full transition-colors">
                    Reactivate
                </button>
            </form>
            @endif
        </div>
    </div>

    {{-- Stat cards --}}
    <div class="grid grid-cols-4 gap-4">
        <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
            <p class="text-white/40 text-xs tracking-widest uppercase mb-2">Points Balance</p>
            <p class="text-3xl font-semibold text-[#D4B84A]">{{ number_format($user->points_balance) }}</p>
            <p class="text-white/25 text-xs mt-1">Expires {{ $user->points_expiry_date?->format('d M Y') }}</p>
        </div>
        <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
            <p class="text-white/40 text-xs tracking-widest uppercase mb-2">Tier</p>
            <p class="text-lg font-semibold text-white">{{ $user->currentTier?->tier?->name ?? 'Invited' }}</p>
            <p class="text-white/25 text-xs mt-1">Since {{ $user->currentTier?->assigned_at?->format('M Y') ?? $user->enrolled_at?->format('M Y') }}</p>
        </div>
        <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
            <p class="text-white/40 text-xs tracking-widest uppercase mb-2">Total Visits</p>
            <p class="text-3xl font-semibold text-white">{{ $user->visits()->count() }}</p>
        </div>
        <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-5">
            <p class="text-white/40 text-xs tracking-widest uppercase mb-2">Status</p>
            <span class="inline-block px-3 py-1 rounded-full text-xs font-medium mt-1
                {{ $user->status === 'active' ? 'bg-green-900/40 text-green-400' : ($user->status === 'suspended' ? 'bg-orange-900/40 text-orange-400' : 'bg-red-900/40 text-red-400') }}">
                {{ ucfirst($user->status) }}
            </span>
            @if($user->suspension_reason)
            <p class="text-white/30 text-xs mt-2 leading-relaxed">{{ $user->suspension_reason }}</p>
            @endif
        </div>
    </div>

    {{-- Points actions + member info --}}
    <div class="grid grid-cols-3 gap-5">

        {{-- Add Points --}}
        <div class="col-span-2 bg-[#111111] border border-[#1e1e1e] rounded-xl p-6">
            <h3 class="text-sm font-medium mb-5">Add Points (Record Visit)</h3>
            @if($errors->any())
            <div class="bg-red-900/20 border border-red-800/50 rounded-lg p-3 mb-4">
                @foreach($errors->all() as $error)
                <p class="text-red-400 text-xs">{{ $error }}</p>
                @endforeach
            </div>
            @endif
            <form method="POST" action="{{ route('admin.points.credit', $user) }}" class="space-y-4">
                @csrf
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Spend Amount ($)</label>
                        <input type="number" name="spend_amount" step="0.01" min="0.01"
                               placeholder="150.00"
                               class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-[#D4B84A] rounded-lg px-4 py-2.5 text-sm text-white outline-none transition-colors placeholder-white/20">
                        <p class="text-white/25 text-xs mt-1">Points = $1 spend = 1 point</p>
                    </div>
                    <div>
                        <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Visit Date</label>
                        <input type="date" name="visit_date" value="{{ date('Y-m-d') }}"
                               class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-[#D4B84A] rounded-lg px-4 py-2.5 text-sm text-white outline-none transition-colors"
                               style="color-scheme: dark;">
                    </div>
                </div>
                <div>
                    <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Admin Note (optional)</label>
                    <input type="text" name="admin_note" placeholder="e.g. Anniversary dinner, Table 4"
                           class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-[#D4B84A] rounded-lg px-4 py-2.5 text-sm text-white outline-none transition-colors placeholder-white/20">
                </div>
                <div class="flex items-center justify-between">
                    <label class="flex items-center gap-2.5 cursor-pointer">
                        <input type="checkbox" name="note_visible_to_member" value="1" class="w-4 h-4 accent-[#D4B84A]">
                        <span class="text-xs text-white/40">Show note to member</span>
                    </label>
                    <button type="submit"
                            class="bg-[#D4B84A] hover:bg-[#E8D080] text-black font-semibold text-xs tracking-widest uppercase px-6 py-2.5 rounded-full transition-colors">
                        Add Points
                    </button>
                </div>
            </form>

            <div class="border-t border-[#1e1e1e] mt-6 pt-5">
                <div class="flex items-center justify-between mb-1">
                    <h4 class="text-sm font-medium text-white/60">Deduct Points</h4>
                    <button @click="debitModal = true"
                            class="border border-red-800/50 hover:border-red-600/60 text-red-400/50 hover:text-red-400 text-xs tracking-wide uppercase px-4 py-2 rounded-full transition-colors">
                        Deduct
                    </button>
                </div>
                <p class="text-white/25 text-xs">Current balance: {{ number_format($user->points_balance) }} pts</p>
            </div>
        </div>

        {{-- Member details --}}
        <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl p-6">
            <h3 class="text-sm font-medium mb-4">Member Details</h3>
            <div class="space-y-4">
                @foreach([
                    ['Email', $user->email],
                    ['Phone', $user->phone ?? '—'],
                    ['DOB', $user->dob?->format('d M Y')],
                    ['Enrolled', $user->enrolled_at?->format('d M Y')],
                ] as [$label, $value])
                <div>
                    <p class="text-white/30 text-xs uppercase tracking-wide">{{ $label }}</p>
                    <p class="text-white text-sm mt-0.5">{{ $value }}</p>
                </div>
                @endforeach
            </div>
        </div>
    </div>

    {{-- Transaction history --}}
    <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">Points History</h3>
            <a href="{{ route('admin.points.history', $user) }}" class="text-[#D4B84A] text-xs hover:text-[#E8D080] transition-colors">View full history →</a>
        </div>
        <div class="divide-y divide-[#1e1e1e]">
            @forelse($recentTransactions as $tx)
            <div class="flex items-center justify-between px-6 py-3.5">
                <div class="flex items-center gap-4">
                    <span class="w-7 h-7 rounded-full flex items-center justify-center text-xs
                        {{ $tx->isCredit() ? 'bg-[#D4B84A]/10 text-[#D4B84A]' : 'bg-red-900/30 text-red-400' }}">
                        {{ $tx->isCredit() ? '+' : '−' }}
                    </span>
                    <div>
                        <p class="text-white text-sm">{{ $tx->description }}</p>
                        <p class="text-white/30 text-xs mt-0.5">
                            {{ $tx->created_at->format('d M Y · h:i A') }}
                            @if($tx->admin) · by {{ $tx->admin->name }} @endif
                            @if($tx->admin_note) · <em class="text-white/40">{{ $tx->admin_note }}</em> @endif
                        </p>
                    </div>
                </div>
                <div class="text-right">
                    <p class="font-semibold text-sm {{ $tx->isCredit() ? 'text-[#D4B84A]' : 'text-red-400' }}">
                        {{ $tx->isCredit() ? '+' : '−' }}{{ number_format($tx->amount) }}
                    </p>
                    <p class="text-white/25 text-xs">Bal: {{ number_format($tx->balance_after) }}</p>
                </div>
            </div>
            @empty
            <div class="px-6 py-8 text-center text-white/30 text-sm">No transactions yet.</div>
            @endforelse
        </div>
    </div>

    {{-- Tier history --}}
    <div class="bg-[#111111] border border-[#1e1e1e] rounded-xl">
        <div class="px-6 py-4 border-b border-[#1e1e1e]">
            <h3 class="text-sm font-medium">Tier History</h3>
        </div>
        <div class="divide-y divide-[#1e1e1e]">
            @forelse($user->tiers as $memberTier)
            <div class="flex items-center justify-between px-6 py-3.5">
                <div class="flex items-center gap-3">
                    <span class="px-2.5 py-1 rounded-full text-xs border
                        {{ $memberTier->tier?->slug === 'limitless' ? 'border-[#D4B84A]/50 text-[#D4B84A]' : ($memberTier->tier?->slug === 'indulged' ? 'border-blue-400/30 text-blue-300' : 'border-white/20 text-white/50') }}">
                        {{ $memberTier->tier?->name }}
                    </span>
                    @if($memberTier->is_current)
                    <span class="text-[10px] text-[#D4B84A] uppercase tracking-widest">Current</span>
                    @endif
                </div>
                <div class="text-right">
                    <p class="text-white/50 text-xs">{{ $memberTier->assigned_at?->format('d M Y') }}</p>
                    <p class="text-white/30 text-xs capitalize">{{ str_replace('_', ' ', $memberTier->assignment_type) }}</p>
                </div>
            </div>
            @empty
            <div class="px-6 py-6 text-center text-white/30 text-sm">No tier history.</div>
            @endforelse
        </div>
    </div>

    {{-- Suspend Modal --}}
    <div x-show="suspendModal" x-cloak
         class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4"
         @keydown.escape.window="suspendModal = false">
        <div class="bg-[#111111] border border-[#2a2a2a] rounded-2xl p-8 w-full max-w-md" @click.stop>
            <h3 class="text-lg font-semibold mb-1" style="font-family: 'Playfair Display', serif;">Suspend Member</h3>
            <p class="text-white/40 text-sm mb-6">{{ $user->full_name }} will not be able to log in while suspended.</p>
            <form method="POST" action="{{ route('admin.members.suspend', $user) }}" class="space-y-4">
                @csrf
                <div>
                    <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Reason *</label>
                    <textarea name="reason" rows="3" required
                              class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-orange-500/50 rounded-lg px-4 py-3 text-sm text-white outline-none transition-colors resize-none"
                              placeholder="Reason for suspension…"></textarea>
                </div>
                <div>
                    <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Suspend Until (optional)</label>
                    <input type="date" name="suspended_until"
                           class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-orange-500/50 rounded-lg px-4 py-3 text-sm text-white outline-none transition-colors"
                           style="color-scheme: dark;">
                </div>
                <div class="flex justify-end gap-3 pt-2">
                    <button type="button" @click="suspendModal = false"
                            class="px-5 py-2.5 rounded-full border border-[#2a2a2a] text-white/50 text-sm hover:text-white transition-colors">
                        Cancel
                    </button>
                    <button type="submit"
                            class="px-5 py-2.5 rounded-full bg-orange-600 hover:bg-orange-500 text-white text-sm font-semibold transition-colors">
                        Suspend
                    </button>
                </div>
            </form>
        </div>
    </div>

    {{-- Debit Modal --}}
    <div x-show="debitModal" x-cloak
         class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4"
         @keydown.escape.window="debitModal = false">
        <div class="bg-[#111111] border border-[#2a2a2a] rounded-2xl p-8 w-full max-w-md" @click.stop>
            <h3 class="text-lg font-semibold mb-1" style="font-family: 'Playfair Display', serif;">Deduct Points</h3>
            <p class="text-white/40 text-sm mb-6">Current balance: <span class="text-white font-medium">{{ number_format($user->points_balance) }} pts</span></p>
            <form method="POST" action="{{ route('admin.points.debit', $user) }}" class="space-y-4">
                @csrf
                <div>
                    <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Points to Deduct *</label>
                    <input type="number" name="amount" min="1" max="{{ $user->points_balance }}" required
                           class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-red-500/50 rounded-lg px-4 py-3 text-sm text-white outline-none transition-colors">
                </div>
                <div>
                    <label class="block text-xs tracking-widest uppercase text-white/40 mb-2">Reason *</label>
                    <textarea name="reason" rows="3" required
                              class="w-full bg-[#1a1a1a] border border-[#2a2a2a] focus:border-red-500/50 rounded-lg px-4 py-3 text-sm text-white outline-none transition-colors resize-none"
                              placeholder="Reason for deduction…"></textarea>
                </div>
                <div class="flex items-center gap-2.5">
                    <input type="checkbox" name="note_visible_to_member" value="1" id="visible_debit" class="w-4 h-4 accent-[#D4B84A]">
                    <label for="visible_debit" class="text-xs text-white/40">Show reason to member</label>
                </div>
                <div class="flex justify-end gap-3 pt-2">
                    <button type="button" @click="debitModal = false"
                            class="px-5 py-2.5 rounded-full border border-[#2a2a2a] text-white/50 text-sm hover:text-white transition-colors">
                        Cancel
                    </button>
                    <button type="submit"
                            class="px-5 py-2.5 rounded-full bg-red-700 hover:bg-red-600 text-white text-sm font-semibold transition-colors">
                        Deduct Points
                    </button>
                </div>
            </form>
        </div>
    </div>

</div>
@endsection
