@extends('layouts.app')

@section('content')
<div class="max-w-7xl mx-auto space-y-8">
    
    <!-- Welcome Banner -->
    <div class="bg-gradient-to-r from-green-900/40 to-gray-900 border border-gray-800 rounded-3xl p-8 flex flex-col md:flex-row justify-between items-center gap-6 shadow-2xl relative overflow-hidden">
        <div class="relative z-10">
            <h1 class="text-3xl font-black text-gray-100">Welcome back, {{ explode(' ', auth()->user()->name)[0] }}! 👋</h1>
            <p class="text-gray-400 mt-2 text-lg">Here is what is happening at <span class="font-bold text-white">{{ auth()->user()->company->name ?? 'your store' }}</span> today.</p>
        </div>
        <div class="relative z-10 flex gap-3">
            @can('process_pos')
            <a href="{{ route('pos.index') }}" class="px-6 py-3 bg-green-600 hover:bg-green-500 text-white font-bold rounded-xl shadow-lg shadow-green-900/20 transition-all flex items-center gap-2">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
                Open Register
            </a>
            @endcan
        </div>
        
        <!-- Decorative Background Icon -->
        <svg class="absolute -right-10 -bottom-10 w-64 h-64 text-green-500/5 rotate-12" fill="currentColor" viewBox="0 0 24 24"><path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"></path></svg>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        
        <!-- LEFT COLUMN: Metrics & Quick Links -->
        <div class="lg:col-span-2 space-y-8">
            
            <!-- Today's Performance -->
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
                <!-- Revenue Card -->
                <div class="bg-gray-900 border border-gray-800 rounded-2xl p-6 shadow-xl relative overflow-hidden group hover:border-gray-700 transition-colors">
                    <p class="text-sm font-medium text-gray-400 mb-1 uppercase tracking-wider">Today's Revenue</p>
                    <h3 class="text-4xl font-black text-green-500 tracking-tight mt-2">
                        TZS {{ number_format($todaysRevenue, 0) }}
                    </h3>
                    <div class="absolute top-6 right-6 w-12 h-12 bg-green-500/10 rounded-full flex items-center justify-center text-green-400 group-hover:scale-110 transition-transform">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                    </div>
                </div>

                <!-- Transactions Card -->
                <div class="bg-gray-900 border border-gray-800 rounded-2xl p-6 shadow-xl relative overflow-hidden group hover:border-gray-700 transition-colors">
                    <p class="text-sm font-medium text-gray-400 mb-1 uppercase tracking-wider">Today's Sales</p>
                    <h3 class="text-4xl font-black text-blue-400 tracking-tight mt-2">
                        {{ number_format($todaysTransactions) }} <span class="text-xl text-gray-500 font-medium">receipts</span>
                    </h3>
                    <div class="absolute top-6 right-6 w-12 h-12 bg-blue-500/10 rounded-full flex items-center justify-center text-blue-400 group-hover:scale-110 transition-transform">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
                    </div>
                </div>
            </div>

            <!-- Quick Actions Grid -->
            <div>
                <h3 class="text-lg font-bold text-gray-100 mb-4 flex items-center gap-2">
                    <svg class="w-5 h-5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
                    Quick Actions
                </h3>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                    @can('view_reports')
                    <a href="{{ route('reports.index') }}" class="p-4 bg-gray-900 border border-gray-800 hover:bg-gray-800 rounded-xl transition-all flex flex-col items-center justify-center gap-3 text-gray-400 hover:text-white group">
                        <div class="p-3 bg-gray-800 group-hover:bg-gray-700 rounded-lg transition-colors">
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"></path></svg>
                        </div>
                        <span class="text-sm font-bold text-center">Financials</span>
                    </a>
                    @endcan
                    
                    @can('manage_inventory')
                    <a href="{{ route('products.index') }}" class="p-4 bg-gray-900 border border-gray-800 hover:bg-gray-800 rounded-xl transition-all flex flex-col items-center justify-center gap-3 text-gray-400 hover:text-white group">
                        <div class="p-3 bg-gray-800 group-hover:bg-gray-700 rounded-lg transition-colors">
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"></path></svg>
                        </div>
                        <span class="text-sm font-bold text-center">Catalog</span>
                    </a>
                    @endcan

                    @can('manage_roles')
                    <a href="{{ route('roles.index') }}" class="p-4 bg-gray-900 border border-gray-800 hover:bg-gray-800 rounded-xl transition-all flex flex-col items-center justify-center gap-3 text-gray-400 hover:text-white group">
                        <div class="p-3 bg-gray-800 group-hover:bg-gray-700 rounded-lg transition-colors">
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                        </div>
                        <span class="text-sm font-bold text-center">Access Roles</span>
                    </a>
                    @endcan
                </div>
            </div>
        </div>

        <!-- RIGHT COLUMN: Alerts -->
        <div class="space-y-6">
            <div class="bg-gray-900 border border-gray-800 rounded-2xl shadow-xl overflow-hidden flex flex-col h-full max-h-[500px]">
                <div class="p-5 border-b border-gray-800 bg-gray-950/50 flex items-center justify-between">
                    <h3 class="text-lg font-bold text-gray-100 flex items-center gap-2">
                        <svg class="w-5 h-5 text-orange-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
                        Low Stock Alerts
                    </h3>
                    <span class="bg-gray-800 text-gray-300 text-xs font-bold px-2 py-1 rounded-lg">{{ $lowStockProducts->count() }}</span>
                </div>
                
                <div class="p-0 overflow-y-auto custom-scrollbar flex-1">
                    @if($lowStockProducts->isEmpty())
                        <div class="p-8 text-center text-gray-500">
                            <svg class="w-12 h-12 mx-auto mb-3 text-gray-700" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                            <p class="font-medium">All items well stocked!</p>
                        </div>
                    @else
                        <ul class="divide-y divide-gray-800">
                            @foreach($lowStockProducts as $product)
                            <li class="p-4 hover:bg-gray-800/30 transition-colors flex justify-between items-center">
                                <div>
                                    <p class="font-bold text-gray-200">{{ $product->name }}</p>
                                    <p class="text-xs text-gray-500 font-mono">{{ $product->sku ?? 'No SKU' }}</p>
                                </div>
                                <div class="text-right">
                                    <span class="inline-flex items-center justify-center px-2.5 py-1 rounded-full text-xs font-bold {{ $product->stock_quantity === 0 ? 'bg-red-500/10 text-red-400 border border-red-500/20' : 'bg-orange-500/10 text-orange-400 border border-orange-500/20' }}">
                                        {{ $product->stock_quantity }} left
                                    </span>
                                </div>
                            </li>
                            @endforeach
                        </ul>
                    @endif
                </div>
                
                @can('manage_inventory')
                <div class="p-4 bg-gray-950 border-t border-gray-800">
                    <a href="{{ route('products.index') }}" class="block w-full py-2.5 text-center bg-gray-800 hover:bg-gray-700 text-gray-300 text-sm font-bold rounded-lg transition-colors">
                        Manage Inventory
                    </a>
                </div>
                @endcan
            </div>
        </div>

    </div>
</div>
@endsection