@extends('layouts.app')

@section('content')
    <div class="max-w-7xl mx-auto space-y-6">
        
        <!-- Welcome Header -->
        <div class="flex items-center justify-between mb-8">
            <div>
                <h2 class="text-2xl font-bold text-gray-100">System Overview</h2>
                <p class="text-sm text-gray-500 mt-1">Welcome back, {{ Auth::user()->name }}. Here is what is happening at {{ Auth::user()->company->name ?? 'your organization' }}.</p>
            </div>
        </div>

        <!-- Metrics Grid -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
            <!-- Total Users Card -->
            <div class="bg-gray-900 border border-gray-800 rounded-2xl p-6 shadow-lg">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm font-medium text-gray-400">Total Active Users</p>
                        <p class="text-3xl font-bold text-gray-100 mt-2">{{ $totalUsers }}</p>
                    </div>
                    <div class="p-3 bg-blue-500/10 rounded-xl text-blue-400">
                        <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
                    </div>
                </div>
            </div>

            <!-- Roles Card -->
            <div class="bg-gray-900 border border-gray-800 rounded-2xl p-6 shadow-lg">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm font-medium text-gray-400">Security Roles</p>
                        <p class="text-3xl font-bold text-gray-100 mt-2">{{ $activeRoles }}</p>
                    </div>
                    <div class="p-3 bg-purple-500/10 rounded-xl text-purple-400">
                        <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4V8a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2z"></path></svg>
                    </div>
                </div>
            </div>

            <!-- System Status -->
            <div class="bg-gray-900 border border-gray-800 rounded-2xl p-6 shadow-lg">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm font-medium text-gray-400">System Status</p>
                        <p class="text-xl font-bold text-green-400 mt-2 flex items-center gap-2">
                            <span class="relative flex h-3 w-3">
                              <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
                              <span class="relative inline-flex rounded-full h-3 w-3 bg-green-500"></span>
                            </span>
                            Operational
                        </p>
                    </div>
                    <div class="p-3 bg-green-500/10 rounded-xl text-green-400">
                        <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                    </div>
                </div>
            </div>
        </div>

        <!-- Recent Logins Feed -->
        <div class="bg-gray-900/50 border border-gray-800 rounded-2xl overflow-hidden shadow-sm mt-8">
            <div class="px-6 py-5 border-b border-gray-800 flex justify-between items-center">
                <h3 class="text-lg font-bold text-gray-100">Recent Security Activity</h3>
                <a href="#" class="text-sm text-green-500 hover:text-green-400 font-medium">View Full Audit Log &rarr;</a>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-left text-sm whitespace-nowrap">
                    <thead class="bg-gray-950/50 text-gray-400 uppercase text-xs tracking-wider font-semibold">
                        <tr>
                            <th class="px-6 py-4">User</th>
                            <th class="px-6 py-4">IP Address</th>
                            <th class="px-6 py-4">Session Started</th>
                            <th class="px-6 py-4">Session Ended</th>
                            <th class="px-6 py-4">Device</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-800 text-gray-300">
                        @forelse($recentLogins as $log)
                            <tr class="hover:bg-gray-800/30 transition-colors">
                                <td class="px-6 py-4 font-medium text-gray-200">
                                    {{ $log->user->name ?? 'Unknown User' }}
                                </td>
                                <td class="px-6 py-4 font-mono text-xs text-blue-400">
                                    {{ $log->ip_address }}
                                </td>
                                <td class="px-6 py-4 text-gray-300">
                                    <div class="text-sm">{{ \Carbon\Carbon::parse($log->login_at)->format('M d, Y') }}</div>
                                    <div class="text-xs text-gray-500">{{ \Carbon\Carbon::parse($log->login_at)->format('h:i:s A') }}</div>
                                </td>
                                <td class="px-6 py-4">
                                    @if($log->logout_at)
                                        <div class="text-sm text-gray-300">{{ \Carbon\Carbon::parse($log->logout_at)->format('M d, Y') }}</div>
                                        <div class="text-xs text-gray-500">{{ \Carbon\Carbon::parse($log->logout_at)->format('h:i:s A') }}</div>
                                    @else
                                        <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-xs font-medium bg-green-500/10 text-green-400 border border-green-500/20">
                                            <span class="w-1.5 h-1.5 rounded-full bg-green-500 animate-pulse"></span> Active Now
                                        </span>
                                    @endif
                                </td>
                                <td class="px-6 py-4 text-gray-500 truncate max-w-[200px]" title="{{ $log->user_agent }}">
                                    {{ \Illuminate\Support\Str::limit($log->user_agent, 40) }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="px-6 py-8 text-center text-gray-500 italic">
                                    No security logs recorded yet. Log out and back in to generate data.
                                </td>
                            </tr>
                        @endempty
                    </tbody>
                </table>
            </div>
        </div>

    </div>
@endsection