@extends('dashboard.app')

@section('page_title', 'User Details')
@section('page_subtitle', 'Profile, access, and subscription history.')

@section('dashboard_content')
    <a href="{{ route('dashboard.admin.users') }}" class="inline-flex items-center gap-2 text-sm text-gray-400 hover:text-white">
        <i data-lucide="arrow-left" class="w-4 h-4"></i><span>Back to users</span>
    </a>

    <div class="mt-6 flex flex-wrap items-center justify-between gap-4">
        <div class="text-sm text-gray-400">User ID: {{ $user->id }}</div>
        <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-4 py-2 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 user</button>
        </form>
    </div>

    <div class="mt-6 grid gap-6 lg:grid-cols-3">
        <div class="rounded-2xl glass-card p-6">
            <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Account</div>
            <div class="mt-3 text-2xl font-semibold text-white">{{ $user->name ?? '—' }}</div>
            <div class="mt-2 text-sm text-gray-400">{{ $user->email }}</div>
        </div>
        <div class="rounded-2xl glass-card p-6">
            <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Access</div>
            <div class="mt-3 text-2xl font-semibold text-white">{{ $user->role ?? 'user' }}</div>
            <div class="mt-2 text-sm text-gray-400">Tier: {{ $user->tier ?? 'free' }}</div>
        </div>
        <div class="rounded-2xl glass-card p-6">
            <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Joined</div>
            <div class="mt-3 text-2xl font-semibold text-white">{{ $user->created_at?->toDateString() ?? '—' }}</div>
            <div class="mt-2 text-sm text-gray-400">{{ $user->created_at?->diffForHumans() ?? '' }}</div>
        </div>
    </div>

    <div class="mt-8 rounded-2xl glass-card p-6">
        <div class="flex items-center justify-between">
            <div>
                <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Subscriptions</div>
                <div class="mt-2 text-lg font-semibold text-white">Recent access records</div>
            </div>
        </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>
                        <th class="py-3 pr-4">Plan</th>
                        <th class="py-3 pr-4">Status</th>
                        <th class="py-3 pr-4">Started</th>
                        <th class="py-3 pr-4">Expires</th>
                        <th class="py-3 pr-4 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/10 text-gray-300">
                    @forelse ($subscriptions as $row)
                        <tr>
                            <td class="py-4 pr-4">{{ $row->plan }}</td>
                            <td class="py-4 pr-4">
                                @php
                                    $inactive = in_array($row->status, ['revoked', 'canceled', 'cancelled', 'suspended'], true);
                                    $pill = $inactive
                                        ? ['bg' => 'bg-rose-100 dark:bg-rose-500/20', 'text' => 'text-rose-800 dark:text-rose-200']
                                        : ($row->status === 'pending'
                                            ? ['bg' => 'bg-amber-100 dark:bg-amber-500/20', 'text' => 'text-amber-800 dark:text-amber-200']
                                            : ['bg' => 'bg-emerald-100 dark:bg-emerald-500/20', 'text' => 'text-emerald-800 dark:text-emerald-200']);
                                @endphp
                                <span class="rounded-full {{ $pill['bg'] }} px-3 py-1 text-xs font-semibold {{ $pill['text'] }}">
                                    {{ $row->status }}
                                </span>
                            </td>
                            <td class="py-4 pr-4 text-xs">{{ $row->started_at?->toDateString() ?? '—' }}</td>
                            <td class="py-4 pr-4 text-xs">{{ $row->expires_at?->toDateString() ?? '—' }}</td>
                            <td class="py-4 pr-4 text-right">
                                <a href="{{ route('dashboard.admin.subscriptions.show', $row->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>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="py-6 text-center text-sm text-gray-400">No subscriptions found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
@endsection
