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

    {{-- Info card --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Stato</p>
                @php $stato = $territorio->stato; @endphp
                <span class="inline-flex items-center mt-1 px-2.5 py-0.5 rounded-full text-xs font-medium
                    {{ match($stato) {
                        'in_reparto' => 'bg-green-100 text-green-800',
                        'assegnato' => 'bg-blue-100 text-blue-800',
                        'da_rientrare' => 'bg-red-100 text-red-800',
                        'inattivo' => 'bg-gray-100 text-gray-600',
                        default => 'bg-gray-100 text-gray-600'
                    } }}">
                    {{ str_replace('_', ' ', ucfirst($stato)) }}
                </span>
                @if($territorio->is_prioritario)
                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-amber-100 text-amber-800 ml-1">★ Prioritario</span>
                @endif
            </div>
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Zona</p>
                <p class="mt-1 text-sm text-gray-900">{{ $territorio->zona?->nome ?? '-' }}</p>
            </div>
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Tipologia</p>
                <p class="mt-1 text-sm text-gray-900">{{ $territorio->tipologia?->nome ?? '-' }}</p>
            </div>
            <div>
                <p class="text-xs font-medium text-gray-500 uppercase">Assegnatario</p>
                <p class="mt-1 text-sm text-gray-900">{{ $territorio->assegnatario?->nome_completo ?? 'Nessuno' }}</p>
            </div>
        </div>

        @if($territorio->note)
        <div class="mt-4 pt-4 border-t">
            <p class="text-xs font-medium text-gray-500 uppercase">Note</p>
            <p class="mt-1 text-sm text-gray-700">{{ $territorio->note }}</p>
        </div>
        @endif

        @if($territorio->confini)
        <div class="mt-4 pt-4 border-t">
            <p class="text-xs font-medium text-gray-500 uppercase">Confini</p>
            <p class="mt-1 text-sm text-gray-700">{{ $territorio->confini }}</p>
        </div>
        @endif
    </div>

    {{-- PDF viewer --}}
    @if($territorio->pdf_path)
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 mb-6">
        <h3 class="text-sm font-semibold text-gray-700 mb-3">PDF Territorio</h3>
        <div class="w-full" style="min-height: 500px;">
            <embed src="{{ asset('storage/' . $territorio->pdf_path) }}" type="application/pdf" width="100%" height="500px">
            <p class="mt-2 text-sm text-gray-500">
                Se il PDF non è visibile:
                <a href="{{ asset('storage/' . $territorio->pdf_path) }}" target="_blank" class="text-indigo-600 hover:underline">Scarica PDF</a>
            </p>
        </div>
    </div>
    @endif

    {{-- 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">Proclamatore</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">{{ $assegnazione->proclamatore?->nome_completo ?? 'N/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">Sì — {{ $assegnazione->campagna?->descrizione }}</span>
                                @elseif($assegnazione->counted_in_campaign === false)
                                    <span class="text-gray-400">No</span>
                                @else
                                    <span class="text-gray-300">-</span>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @empty
        <p class="text-gray-500 text-sm">Nessuna assegnazione registrata.</p>
        @endforelse
    </div>
</div>
