<div>
    <div class="mb-6 flex items-center justify-between">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">{{ $proclamatore->nome_completo }}</h1>
            <a href="{{ route('proclamatori.index') }}" class="text-sm text-indigo-600 hover:text-indigo-800">← Torna alla lista</a>
        </div>
        @can('proclamatori.manage')
        <a href="{{ route('proclamatori.edit', $proclamatore) }}" class="px-4 py-2 text-sm font-medium text-white bg-indigo-600 rounded-lg hover:bg-indigo-700 transition">Modifica</a>
        @endcan
    </div>

    {{-- Stats --}}
    <div class="grid grid-cols-1 sm:grid-cols-3 gap-4 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <p class="text-xs font-medium text-gray-500 uppercase">Stato</p>
            @if($proclamatore->attivo)
                <span class="inline-flex mt-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">Attivo</span>
            @else
                <span class="inline-flex mt-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-600">Inattivo</span>
            @endif
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <p class="text-xs font-medium text-gray-500 uppercase">Territori Attualmente</p>
            <p class="mt-1 text-2xl font-bold text-indigo-600">{{ $stats['attualmente_assegnati'] }}</p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <p class="text-xs font-medium text-gray-500 uppercase">Media Giorni Trattenuta</p>
            <p class="mt-1 text-2xl font-bold text-gray-900">{{ $stats['media_giorni'] }} gg</p>
            <p class="text-xs text-gray-500">su {{ $stats['totale_assegnazioni'] }} assegnazioni totali</p>
        </div>
    </div>

    {{-- Assignment history --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <h3 class="text-lg font-semibold text-gray-900 mb-4">Storico Assegnazioni</h3>

        @forelse($assegnazioniPerAnno as $annoLabel => $assegnazioni)
        <div class="mb-6">
            <h4 class="text-sm font-semibold text-indigo-600 mb-2">Anno Teocratico {{ $annoLabel }}</h4>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200 text-sm">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">Territorio</th>
                            <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">Assegnato</th>
                            <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">Rientrato</th>
                            <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">Giorni</th>
                            <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">Campagna</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        @foreach($assegnazioni as $assegnazione)
                        <tr>
                            <td class="px-3 py-2">
                                <a href="{{ route('territori.show', $assegnazione->territorio_id) }}" class="text-indigo-600 hover:underline">
                                    {{ $assegnazione->territorio?->numero ?? 'N/A' }}
                                </a>
                            </td>
                            <td class="px-3 py-2">{{ $assegnazione->assigned_at->format('d/m/Y') }}</td>
                            <td class="px-3 py-2">
                                @if($assegnazione->returned_at)
                                    {{ $assegnazione->returned_at->format('d/m/Y') }}
                                @else
                                    <span class="text-amber-600 font-medium">In corso</span>
                                @endif
                            </td>
                            <td class="px-3 py-2">{{ $assegnazione->giorni }}</td>
                            <td class="px-3 py-2">
                                @if($assegnazione->counted_in_campaign)
                                    <span class="text-green-600">{{ $assegnazione->campagna?->descrizione }}</span>
                                @else
                                    <span class="text-gray-400">-</span>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @empty
        <p class="text-gray-500 text-sm">Nessuna assegnazione registrata.</p>
        @endforelse
    </div>
</div>
