@extends('layouts.admin')

@section('title', 'Leaderboard')

@section('content')
    <div class="d-flex flex-wrap justify-content-between align-items-center gap-1 mb-2">
        <div>
            <span class="loot-status success">Rewards</span>
            <h1 class="loot-section-title mt-1 mb-25">Leaderboard rewards</h1>
            <p class="loot-muted mb-0">Set prize amounts per rank and hand them to the current top players for the selected period.</p>
        </div>
        <form method="GET" class="d-flex align-items-center gap-1">
            <select name="period" class="form-select" onchange="this.form.submit()">
                @foreach(['daily'=>'Daily','weekly'=>'Weekly','monthly'=>'Monthly','all_time'=>'All time'] as $value => $label)
                    <option value="{{ $value }}" {{ $period === $value ? 'selected' : '' }}>{{ $label }}</option>
                @endforeach
            </select>
            <select name="limit" class="form-select" onchange="this.form.submit()">
                @foreach([25,30,35,40,45,50] as $option)
                    <option value="{{ $option }}" {{ $limit === $option ? 'selected' : '' }}>Top {{ $option }}</option>
                @endforeach
            </select>
        </form>
    </div>

    @if(session('success'))<div class="alert alert-success"><div class="alert-body">{{ session('success') }}</div></div>@endif
    @if(session('error'))<div class="alert alert-danger"><div class="alert-body">{{ session('error') }}</div></div>@endif

    <div class="row">
        <div class="col-lg-4 mb-2">
            <section class="user-page-card h-100">
                <h3 class="loot-section-title mb-1"><i data-feather="award" class="me-50"></i>Rank prizes</h3>
                <p class="loot-muted">Coins awarded to each rank when you distribute. Leave blank/0 to skip a rank.</p>
                <form method="POST" action="{{ route('admin.leaderboard.prizes', ['period' => $period, 'limit' => $limit]) }}">
                    @csrf
                    @for($rank = 1; $rank <= min($limit, 10); $rank++)
                        <div class="d-flex align-items-center gap-1 mb-1">
                            <span class="badge badge-light-primary" style="min-width:52px;">#{{ $rank }}</span>
                            <input type="number" min="0" step="1" name="prizes[{{ $rank }}]" class="form-control"
                                   value="{{ $prizes[$rank] ?? '' }}" placeholder="0">
                            <span class="loot-muted">coins</span>
                        </div>
                    @endfor
                    <button class="btn btn-loot w-100 mt-1">Save prizes</button>
                </form>
            </section>
        </div>

        <div class="col-lg-8 mb-2">
            <section class="user-page-card h-100">
                <div class="d-flex flex-wrap justify-content-between align-items-center gap-1 mb-1">
                    <h3 class="loot-section-title mb-0"><i data-feather="bar-chart-2" class="me-50"></i>Top players — {{ $periodLabel }}</h3>
                    <form method="POST" action="{{ route('admin.leaderboard.distribute') }}"
                          onsubmit="return confirm('Distribute the configured rank prizes to these players now?')">
                        @csrf
                        <input type="hidden" name="period" value="{{ $period }}">
                        <input type="hidden" name="limit" value="{{ $limit }}">
                        <button class="btn btn-loot" {{ (empty($prizes) || $players->isEmpty()) ? 'disabled' : '' }}>
                            <i data-feather="gift" class="me-50"></i>Distribute prizes
                        </button>
                    </form>
                </div>

                <div class="table-responsive admin-leads-table-wrap">
                    <table class="table admin-leads-table mb-0">
                        <thead><tr><th>Rank</th><th>Player</th><th>Points ({{ $period === 'all_time' ? 'lifetime' : 'period' }})</th><th>Prize</th><th>Status</th></tr></thead>
                        <tbody>
                            @forelse($players as $player)
                                <tr>
                                    <td><strong>#{{ $player->leaderboard_rank }}</strong></td>
                                    <td>{{ $player->username }}</td>
                                    <td>{{ number_format((float) $player->period_points) }}</td>
                                    <td>{{ isset($prizes[$player->leaderboard_rank]) ? number_format($prizes[$player->leaderboard_rank]).' coins' : '—' }}</td>
                                    <td>
                                        @if(in_array($player->id, $alreadyAwarded))
                                            <span class="badge badge-light-success">Awarded</span>
                                        @elseif(isset($prizes[$player->leaderboard_rank]))
                                            <span class="badge badge-light-warning">Pending</span>
                                        @else
                                            <span class="badge badge-light-secondary">No prize</span>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr><td colspan="5" class="text-center loot-muted py-3">No ranked players for this period yet.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </section>
        </div>
    </div>

    <section class="user-page-card">
        <h3 class="loot-section-title mb-1"><i data-feather="clock" class="me-50"></i>Recent awards</h3>
        <div class="table-responsive admin-leads-table-wrap">
            <table class="table admin-leads-table mb-0">
                <thead><tr><th>Player</th><th>Rank</th><th>Prize</th><th>Period</th><th>When</th></tr></thead>
                <tbody>
                    @forelse($recentAwards as $award)
                        <tr>
                            <td>{{ optional($award->user)->username ?? '—' }}</td>
                            <td>#{{ $award->rank }}</td>
                            <td>{{ number_format((float) $award->points) }} coins</td>
                            <td>{{ $award->period_label }}</td>
                            <td>{{ $award->created_at?->diffForHumans() }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center loot-muted py-3">No prizes awarded yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </section>
@endsection
