@extends('layouts.admin')

@section('title', 'Dashboard')

@section('content')
<div class="mb-8">
    <h1 class="text-2xl font-bold text-slate-900 tracking-tight">Welcome back, {{ Auth::guard('admin')->user()->username ?? 'Admin' }}! 🎉</h1>
    <p class="text-slate-500 mt-1 text-base">Here's an overview of your website content and quick tools.</p>
</div>

<!-- Statistics Grid -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-4 gap-5 mb-8">
    @php
    $stats = [
        ['label' => 'Divisions', 'count' => $division_count, 'link' => route('admin.divisions.index'), 'color' => 'blue', 'icon' => '<path d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"/>'],
        ['label' => 'News Posts', 'count' => $post_count, 'link' => route('admin.posts.index'), 'color' => 'indigo', 'icon' => '<path d="M19 20H5a2 2 0 01-2-2V6a2 2 0 012-2h10a2 2 0 012 2v1m2 13a2 2 0 01-2-2V7m2 13a2 2 0 002-2V9a2 2 0 00-2-2h-2m-4-3H9M7 16h6M7 8h6v4H7V8z"/>'],
        ['label' => 'Media Assets', 'count' => $media_count, 'link' => route('admin.media.index'), 'color' => 'emerald', 'icon' => '<path d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>'],
        ['label' => 'Messages', 'count' => $message_count, 'link' => route('admin.messages.index'), 'color' => 'rose', 'icon' => '<path d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>'],
    ];
    @endphp

    @foreach ($stats as $stat)
    <a href="{{ $stat['link'] }}" class="bg-white p-5 rounded-2xl shadow-sm border border-slate-100 hover:shadow-md hover:-translate-y-0.5 transition-all duration-300 group flex flex-col justify-between">
        <div class="flex items-start justify-between mb-4">
            <div class="w-12 h-12 rounded-xl flex items-center justify-center transition-all duration-300 shadow-sm border
                 bg-{{ $stat['color'] }}-50 text-{{ $stat['color'] }}-500 
                 group-hover:bg-{{ $stat['color'] }}-500 group-hover:text-white border-{{ $stat['color'] }}-100 group-hover:border-transparent">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">{!! $stat['icon'] !!}</svg>
            </div>
            <div class="w-8 h-8 rounded-full bg-slate-50 flex items-center justify-center text-slate-300 group-hover:bg-brand-primary group-hover:text-white transition-all">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M14 5l7 7m0 0l-7 7m7-7H3"/></svg>
            </div>
        </div>
        <div>
            <p class="text-3xl font-extrabold text-slate-900 tracking-tight">{{ $stat['count'] }}</p>
            <p class="text-[11px] text-slate-500 font-bold uppercase tracking-widest mt-1">{{ $stat['label'] }}</p>
        </div>
    </a>
    @endforeach
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-8 mb-8">
    <!-- Activity Feed -->
    <div class="lg:col-span-2 space-y-8">
        <div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">
            <div class="p-6 border-b border-slate-50 flex items-center justify-between bg-slate-50/50">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-xl bg-brand-primary/10 text-brand-primary flex items-center justify-center">
                        <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="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                    </div>
                    <h2 class="text-lg font-bold text-slate-900">Recent Activity</h2>
                </div>
                <a href="{{ route('admin.activities') }}" class="text-sm font-bold text-brand-primary hover:text-brand-accent transition flex items-center gap-1">
                    View All
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M9 5l7 7-7 7"/></svg>
                </a>
            </div>
            <div class="divide-y divide-slate-50">
                @forelse($activities as $activity)
                <div class="p-5 hover:bg-slate-50/80 transition-all flex items-start gap-4 group">
                    <div class="w-10 h-10 rounded-xl bg-slate-100 text-slate-400 flex items-center justify-center group-hover:bg-brand-primary group-hover:text-white transition-colors shrink-0">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">{!! $activity->activity_icon !!}</svg>
                    </div>
                    <div class="flex-1 min-w-0">
                        <div class="flex items-center justify-between gap-2">
                            <p class="text-sm font-bold text-slate-900 truncate tracking-tight">{{ $activity->activity_label }}</p>
                            <span class="text-[10px] text-slate-400 font-medium shrink-0">{{ $activity->created_at->diffForHumans() }}</span>
                        </div>
                        <div class="flex items-center gap-2 mt-1">
                            <span class="text-[10px] px-1.5 py-0.5 rounded bg-slate-100 text-slate-500 font-bold uppercase tracking-tighter">{{ $activity->activity_type }}</span>
                            @if($activity->activity_type == 'Message')
                                <p class="text-xs text-slate-500 italic truncate">"{{ \Illuminate\Support\Str::limit($activity->message, 60) }}"</p>
                            @endif
                        </div>
                    </div>
                </div>
                @empty
                <div class="p-10 text-center">
                    <p class="text-slate-400">No activity recorded yet.</p>
                </div>
                @endforelse
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            <!-- Quick Actions Panel -->
            <div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
                <div class="flex items-center gap-3 mb-6">
                    <div class="w-8 h-8 rounded-lg bg-brand-primary/10 flex items-center justify-center text-brand-primary">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M13 10V3L4 14h7v7l9-11h-7z"/></svg>
                    </div>
                    <h2 class="text-lg font-bold text-slate-900">Quick Actions</h2>
                </div>
                
                <div class="grid grid-cols-2 gap-3">
                    @php
                        $actions = [
                            ['label' => 'New Division', 'link' => route('admin.divisions.create')],
                            ['label' => 'New Portfolio', 'link' => route('admin.portfolio.create')],
                            ['label' => 'New Team', 'link' => route('admin.team.create')],
                            ['label' => 'New Post', 'link' => route('admin.posts.create')],
                            ['label' => 'New Hero Slide', 'link' => route('admin.hero-slides.create')],
                        ];
                    @endphp

                    @foreach($actions as $action)
                    <a href="{{ $action['link'] }}" class="flex flex-col items-center justify-center gap-2 p-4 bg-slate-50 border border-slate-100 rounded-xl text-slate-700 hover:bg-brand-primary hover:text-white hover:border-brand-primary transition-all group shadow-sm hover:shadow-lg hover:shadow-brand-primary/20">
                        <svg class="w-6 h-6 text-slate-300 group-hover:text-brand-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                        </svg>
                        <span class="text-xs font-bold text-center tracking-tight">{{ $action['label'] }}</span>
                    </a>
                    @endforeach
                </div>
            </div>

            <!-- Enhanced System Status -->
            <div class="bg-slate-900 rounded-2xl p-6 text-white flex flex-col justify-between">
                <div>
                    <h2 class="text-lg font-bold mb-4">System Status</h2>
                    
                    <div class="space-y-4">
                        <div class="flex items-center justify-between pb-3 border-b border-slate-800">
                            <span class="text-xs text-slate-400 font-bold uppercase tracking-wider">Storage Free</span>
                            <span class="text-sm font-mono text-brand-accent">{{ $system_info['disk_free'] }}</span>
                        </div>
                        <div class="flex items-center justify-between pb-3 border-b border-slate-800">
                            <span class="text-xs text-slate-400 font-bold uppercase tracking-wider">Database</span>
                            <span class="text-sm font-mono">{{ $system_info['db_driver'] }}</span>
                        </div>
                        <div class="flex items-center justify-between pb-3 border-b border-slate-800">
                            <span class="text-xs text-slate-400 font-bold uppercase tracking-wider">Environment</span>
                            <span class="text-sm font-mono {{ $system_info['env'] === 'production' ? 'text-green-400' : 'text-yellow-400' }}">{{ strtoupper($system_info['env']) }}</span>
                        </div>
                        <div class="flex items-center justify-between">
                            <span class="text-xs text-slate-400 font-bold uppercase tracking-wider">Framework</span>
                            <span class="text-sm font-mono">{{ $system_info['laravel_version'] }} (PHP {{ $system_info['php_version'] }})</span>
                        </div>
                    </div>
                </div>
                
                @if(Auth::guard('admin')->user()->isSuperAdmin())
                <a href="{{ route('admin.settings.index') }}" class="mt-8 flex items-center justify-center gap-2 py-3.5 bg-brand-primary rounded-xl font-bold text-sm tracking-tight transition hover:bg-brand-accent shadow-xl shadow-brand-primary/20">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path></svg>
                    Site Settings
                </a>
                @endif
            </div>
        </div>
    </div>

    <!-- Recent Media Right Sidebar -->
    <div class="space-y-8">
        <div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden h-full flex flex-col">
            <div class="p-6 border-b border-slate-50 flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-xl bg-emerald-50 text-emerald-500 flex items-center justify-center">
                        <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="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                    </div>
                    <h2 class="text-lg font-bold text-slate-900">Recent Media</h2>
                </div>
            </div>
            <div class="p-6 grid grid-cols-2 gap-4">
                @forelse($recent_media as $media)
                <div class="aspect-square rounded-xl bg-slate-50 border border-slate-100 overflow-hidden relative group">
                    @if(\Illuminate\Support\Str::startsWith($media->file_type, 'image/'))
                        <img src="{{ $media->url }}" alt="{{ $media->filename }}" class="w-full h-full object-cover">
                    @else
                        <div class="w-full h-full flex flex-col items-center justify-center p-2 text-center">
                            <svg class="w-8 h-8 text-slate-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"/></svg>
                            <span class="text-[10px] text-slate-400 mt-1 truncate w-full">{{ $media->filename }}</span>
                        </div>
                    @endif
                    <div class="absolute inset-0 bg-slate-900/60 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center backdrop-blur-[2px]">
                        <a href="{{ route('admin.media.index') }}" class="p-2 bg-white rounded-lg text-slate-900 shadow-xl">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M24 12s-4.5-9-12-9-12 9-12 9 4.5 9 12 9 12-9 12-9z"/></svg>
                        </a>
                    </div>
                </div>
                @empty
                <div class="col-span-2 py-10 text-center">
                    <p class="text-slate-400 text-sm">No uploads yet.</p>
                </div>
                @endforelse
            </div>
            <div class="p-6 pt-0 mt-auto">
                <a href="{{ route('admin.media.index') }}" class="w-full flex items-center justify-center gap-2 py-3 bg-slate-50 hover:bg-slate-100 text-slate-600 font-bold rounded-xl text-sm transition">
                    Open Media Library
                </a>
            </div>
        </div>
    </div>
</div>
@endsection

