@extends('layouts.admin')

@section('title', 'Sync Logs')
@section('page-title', 'Sync Logs')

@section('content')
<div class="space-y-6">
    {{-- Stats --}}
    <section class="grid gap-4 md:grid-cols-3">
        <div class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm"><p class="text-sm font-semibold uppercase tracking-[0.24em] text-slate-500">Today</p><p class="mt-4 text-4xl font-black text-slate-900">{{ number_format($stats['total_today']) }}</p></div>
        <div class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm"><p class="text-sm font-semibold uppercase tracking-[0.24em] text-slate-500">Failed Today</p><p class="mt-4 text-4xl font-black text-brand-600">{{ number_format($stats['failed_today']) }}</p></div>
        <div class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm"><p class="text-sm font-semibold uppercase tracking-[0.24em] text-slate-500">Last 7 Days</p><p class="mt-4 text-4xl font-black text-accent-600">{{ number_format($stats['total_week']) }}</p></div>
    </section>

    {{-- Search --}}
    <section class="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
        <form method="GET" class="flex gap-4">
            <input type="text" name="q" value="{{ request('q') }}" placeholder="Search by user name or email"
                class="w-full rounded-2xl border border-slate-300 px-4 py-3 text-sm outline-none transition focus:border-brand-600 focus:ring-4 focus:ring-brand-600/10">
            <button type="submit" class="rounded-2xl bg-brand-600 px-5 py-3 text-sm font-semibold text-white transition hover:bg-brand-700">Search</button>
            @if(request('q'))
                <a href="{{ route('admin.sync-logs.index') }}" class="rounded-2xl border border-slate-300 px-5 py-3 text-sm font-semibold text-slate-600 hover:bg-slate-50">Clear</a>
            @endif
        </form>
    </section>

    {{-- Grouped user cards --}}
    <section class="rounded-3xl border border-slate-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full text-left text-sm">
                <thead class="border-b border-slate-200 bg-slate-50 text-slate-500">
                    <tr>
                        <th class="px-6 py-4 font-semibold">User</th>
                        <th class="px-6 py-4 font-semibold text-center">Total Syncs</th>
                        <th class="px-6 py-4 font-semibold text-center">Uploads</th>
                        <th class="px-6 py-4 font-semibold text-center">Downloads</th>
                        <th class="px-6 py-4 font-semibold text-center">Failed</th>
                        <th class="px-6 py-4 font-semibold text-center">Records</th>
                        <th class="px-6 py-4 font-semibold">Last Sync</th>
                        <th class="px-6 py-4 font-semibold"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($grouped as $row)
                        <tr class="table-row-hover">
                            <td class="px-6 py-5">
                                <div class="font-semibold text-slate-900">{{ $row->user->name ?? 'Unknown' }}</div>
                                <div class="text-xs text-slate-500">{{ $row->user->email ?? '—' }}</div>
                                @if($row->user?->plan)
                                    <span class="mt-1 inline-block rounded-full bg-brand-50 px-2 py-0.5 text-xs font-semibold text-brand-700">{{ ucfirst($row->user->plan) }}</span>
                                @endif
                            </td>
                            <td class="px-6 py-5 text-center font-bold text-slate-900">{{ number_format($row->total_syncs) }}</td>
                            <td class="px-6 py-5 text-center text-accent-600 font-semibold">↑ {{ number_format($row->uploads) }}</td>
                            <td class="px-6 py-5 text-center text-brand-600 font-semibold">↓ {{ number_format($row->downloads) }}</td>
                            <td class="px-6 py-5 text-center">
                                @if($row->failed > 0)
                                    <span class="rounded-full bg-red-50 px-2 py-1 text-xs font-bold text-red-600">{{ number_format($row->failed) }}</span>
                                @else
                                    <span class="text-slate-400">—</span>
                                @endif
                            </td>
                            <td class="px-6 py-5 text-center text-slate-600">{{ number_format($row->total_records) }}</td>
                            <td class="px-6 py-5 text-slate-500 text-sm">
                                {{ $row->last_sync_at ? \Carbon\Carbon::parse($row->last_sync_at)->diffForHumans() : '—' }}
                            </td>
                            <td class="px-6 py-5">
                                <a href="{{ route('admin.sync-logs.show', $row->user_id) }}"
                                   class="inline-flex items-center gap-1.5 rounded-xl bg-slate-100 px-3 py-2 text-xs font-semibold text-slate-700 transition hover:bg-brand-600 hover:text-white">
                                    View Logs
                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"/></svg>
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="px-6 py-16 text-center text-sm text-slate-500">No sync logs found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="border-t border-slate-200 px-6 py-4">{{ $grouped->links() }}</div>
    </section>
</div>
@endsection
