@extends('layouts.admin')

@section('title', 'Classements')

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0"><i class="bi bi-trophy me-2"></i>Classements</h1>
        <div class="d-flex gap-2">
            <form action="{{ route('admin.leaderboards.calculate') }}" method="POST" class="d-inline">
                @csrf
                <input type="hidden" name="type" value="all">
                <input type="hidden" name="period" value="{{ $period }}">
                <button type="submit" class="btn btn-warning">
                    <i class="bi bi-arrow-clockwise me-1"></i>Recalculer tous
                </button>
            </form>
            <form action="{{ route('admin.leaderboards.reset') }}" method="POST" class="d-inline" onsubmit="return confirm('Êtes-vous sûr de vouloir réinitialiser ces classements ?');">
                @csrf
                <input type="hidden" name="type" value="{{ $type }}">
                <input type="hidden" name="period" value="{{ $period }}">
                <button type="submit" class="btn btn-outline-danger">
                    <i class="bi bi-trash me-1"></i>Réinitialiser
                </button>
            </form>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    {{-- Statistiques --}}
    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center">
                    <h6 class="text-muted">Total entrées</h6>
                    <h3 class="mb-0">{{ $stats['total_entries'] ?? 0 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center">
                    <h6 class="text-muted">Catégories</h6>
                    <h3 class="mb-0">{{ $stats['types_count'] ?? 0 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center">
                    <h6 class="text-muted">Période actuelle</h6>
                    <h3 class="mb-0">{{ $periods[$period] ?? $period }}</h3>
                </div>
            </div>
        </div>
    </div>

    <div class="card mb-4 border-0 shadow-sm">
        <div class="card-body">
            <form action="{{ route('admin.leaderboards.index') }}" method="GET" class="row g-3 align-items-end">
                <div class="col-md-4">
                    <label class="form-label">Type de classement</label>
                    <select name="type" class="form-select">
                        @foreach($types as $key => $label)
                            <option value="{{ $key }}" {{ $type === $key ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Période</label>
                    <select name="period" class="form-select">
                        @foreach($periods as $key => $label)
                            <option value="{{ $key }}" {{ $period === $key ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-search me-1"></i>Filtrer
                    </button>
                </div>
                <div class="col-md-2">
                    <form action="{{ route('admin.leaderboards.calculate') }}" method="POST" class="d-inline">
                        @csrf
                        <input type="hidden" name="type" value="{{ $type }}">
                        <input type="hidden" name="period" value="{{ $period }}">
                        <button type="submit" class="btn btn-outline-warning w-100">
                            <i class="bi bi-arrow-clockwise me-1"></i>Recalculer
                        </button>
                    </form>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white">
            <h5 class="mb-0">
                <i class="bi bi-list-ol me-2"></i>{{ $types[$type] ?? $type }} - {{ $periods[$period] ?? $period }}
            </h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th width="80">Rang</th>
                            <th>Utilisateur</th>
                            <th>Score</th>
                            <th>Pays</th>
                            <th>Période</th>
                            <th>Détails</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($leaderboards as $entry)
                        <tr class="{{ $entry->rank <= 3 ? 'table-warning' : '' }}">
                            <td>
                                @if($entry->rank === 1)
                                    <span class="badge bg-warning text-dark fs-6">🥇 #1</span>
                                @elseif($entry->rank === 2)
                                    <span class="badge bg-secondary fs-6">🥈 #2</span>
                                @elseif($entry->rank === 3)
                                    <span class="badge bg-dark fs-6">🥉 #3</span>
                                @else
                                    <span class="text-muted">#{{ $entry->rank }}</span>
                                @endif
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="avatar-sm bg-light rounded-circle d-flex align-items-center justify-content-center me-2">
                                        <i class="bi bi-person"></i>
                                    </div>
                                    <div>
                                        <strong>{{ $entry->user->name ?? 'N/A' }}</strong>
                                        <br><small class="text-muted">{{ $entry->user->email ?? '' }}</small>
                                    </div>
                                </div>
                            </td>
                            <td><strong class="text-success">{{ number_format($entry->score, 0, ',', ' ') }}</strong></td>
                            <td>{{ $entry->country ?? '-' }}</td>
                            <td>
                                @if($entry->period_start && $entry->period_end)
                                    {{ $entry->period_start->format('d/m') }} - {{ $entry->period_end->format('d/m/Y') }}
                                @else
                                    -
                                @endif
                            </td>
                            <td>
                                @if($entry->metadata)
                                    <small class="text-muted">
                                        @foreach($entry->metadata as $key => $value)
                                            {{ str_replace('_', ' ', ucfirst($key)) }}: {{ is_array($value) ? json_encode($value) : $value }}<br>
                                        @endforeach
                                    </small>
                                @else
                                    -
                                @endif
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <i class="bi bi-inbox fs-1 text-muted d-block mb-2"></i>
                                Aucun classement disponible pour cette période
                                <br><small class="text-muted">Cliquez sur "Recalculer" pour générer les classements</small>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($leaderboards->hasPages())
        <div class="card-footer bg-white">
            {{ $leaderboards->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
