@extends('layouts.app')
@section('title','Records por Categoria')
@section('content')

@include('components.seletor-contest')

<div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:.5rem;margin-bottom:1rem">
    <div>
        <h2 style="margin:0">🏆 Records por Categoria</h2>
        <p style="margin:.25rem 0 0;color:var(--muted);font-size:.82rem">
            Comparativo entre o record anterior e o melhor resultado do contest selecionado.
        </p>
    </div>
    <div class="stats-grid" style="max-width:380px;margin:0">
        <div class="stat-card">
            <div class="stat-number">{{ number_format($total) }}</div>
            <div class="stat-label">Categorias</div>
        </div>
        <div class="stat-card stat-card-green">
            <div class="stat-number">{{ number_format($quebrados) }}</div>
            <div class="stat-label">Records Quebrados</div>
        </div>
    </div>
</div>

{{-- Legenda --}}
<div style="margin-bottom:1rem;font-size:.82rem;display:flex;gap:1.25rem;flex-wrap:wrap">
    <span style="display:inline-flex;align-items:center;gap:.35rem">
        <span style="background:#e8f5e9;border:1px solid #a5d6a7;width:14px;height:14px;
                     display:inline-block;border-radius:2px"></span>
        Record quebrado (atual &gt; anterior)
    </span>
    <span style="display:inline-flex;align-items:center;gap:.35rem">
        <span style="background:#fff3e0;border:1px solid #ffcc80;width:14px;height:14px;
                     display:inline-block;border-radius:2px"></span>
        Sem record anterior
    </span>
    <span style="display:inline-flex;align-items:center;gap:.35rem">
        <span style="background:#e3f2fd;border:1px solid #90caf9;width:14px;height:14px;
                     display:inline-block;border-radius:2px"></span>
        Resultado importado de <code>resultados_final</code>
    </span>
</div>

{{-- Busca --}}
<form method="GET" action="{{ route('relatorios.records') }}"
      style="display:flex;gap:.5rem;align-items:center;margin-bottom:1.25rem">
    <input type="text" name="q" value="{{ $busca }}"
           placeholder="Buscar categoria ou callsign..."
           style="padding:.4rem .7rem;border:1px solid #ddd;border-radius:var(--radius);
                  font-size:.88rem;min-width:260px">
    <button type="submit" class="btn btn-secondary" style="padding:.4rem .9rem">Buscar</button>
    @if($busca)
        <a href="{{ route('relatorios.records') }}"
           class="btn btn-secondary" style="padding:.4rem .9rem">✕ Limpar</a>
    @endif
    <span style="font-size:.82rem;color:var(--muted)">{{ number_format($total) }} registro(s)</span>
</form>

@if($total === 0)
    <p style="color:#aaa;text-align:center;padding:2rem">Nenhum record encontrado.</p>
@else

<div style="overflow-x:auto">
<table style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--blue);color:#fff">
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;width:40px;text-align:center">SEQ.</th>
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80">Categoria</th>
            {{-- Record Anterior --}}
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;background:#1a3f6f">Callsign Anterior</th>
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;background:#1a3f6f">Nome Anterior</th>
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;background:#1a3f6f;text-align:right">Record Anterior</th>
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;width:24px"></th>
            {{-- Record Atual --}}
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;background:#1a5c2a">Callsign Atual</th>
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;background:#1a5c2a">Nome Atual</th>
            <th style="padding:.4rem .6rem;border:1px solid #1a4a80;background:#1a5c2a;text-align:right">Record Atual</th>
        </tr>
    </thead>
    <tbody>
        @foreach($records as $i => $r)
        @php
            $scoreAtu = $r->final_score_atu_display ?? 0;
            $scoreAnt = $r->final_score_ant ?? 0;
            $callAtu  = $r->callsign_atu_display ?? '';
            $nameAtu  = $r->name_atu_display ?? '';
            $quebrou  = $scoreAtu > $scoreAnt;
            $semAnt   = $scoreAnt == 0;
            $importado= ($r->fonte_atu ?? '') === 'resultados_final';
            $bg = $quebrou ? '#e8f5e9' : ($semAnt ? '#fff3e0' : ($importado ? '#e3f2fd' : '#fff'));
        @endphp
        <tr style="background:{{ $bg }}">
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0;text-align:center;color:#888">{{ $i + 1 }}</td>
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0;font-size:.75rem">{{ $r->category }}</td>
            {{-- Anterior --}}
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0;color:#555">
                {{ $r->callsign_ant ?: '—' }}
            </td>
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0;color:#555;font-size:.75rem">
                {{ $r->name_ant ?: '—' }}
            </td>
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0;text-align:right;color:#555">
                {{ $scoreAnt ? number_format($scoreAnt) : '—' }}
            </td>
            {{-- Seta --}}
            <td style="padding:.32rem .2rem;border:1px solid #e0e0e0;text-align:center;font-size:.9rem">
                @if($quebrou)
                    <span style="color:#2E7D32;font-weight:bold" title="Record quebrado!">▶</span>
                @elseif($scoreAnt > 0 && $scoreAtu == $scoreAnt)
                    <span style="color:#888">=</span>
                @else
                    <span style="color:#aaa">·</span>
                @endif
            </td>
            {{-- Atual --}}
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0">
                <strong>{{ $callAtu ?: '—' }}</strong>
                @if($importado && $callAtu)
                    <span title="Resultado importado de resultados_final"
                          style="font-size:.68rem;color:#1565C0;margin-left:.2rem">📥</span>
                @endif
            </td>
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0;font-size:.75rem">
                {{ $nameAtu ?: '—' }}
            </td>
            <td style="padding:.32rem .6rem;border:1px solid #e0e0e0;text-align:right;
                {{ $quebrou ? 'font-weight:bold;color:#2E7D32' : 'color:var(--blue)' }}">
                {{ $scoreAtu ? number_format($scoreAtu) : '—' }}
            </td>
        </tr>
        @endforeach
    </tbody>
    <tfoot>
        <tr style="background:#f5f5f5;font-weight:bold">
            <td colspan="9" style="padding:.5rem .6rem;border:1px solid #ddd;font-size:.85rem">
                Total: {{ $total }} categoria(s) &nbsp;|&nbsp;
                <span style="color:#2E7D32">{{ $quebrados }} record(s) quebrado(s)</span>
                &nbsp;|&nbsp;
                <span style="color:#888;font-size:.8rem">
                    📥 = resultado direto de <code>resultados_final</code> (Etapa 10 pendente)
                </span>
            </td>
        </tr>
    </tfoot>
</table>
</div>

@endif
@endsection
