@extends('layouts.app')

@section('title', 'Fila de Atendimento — ImóvelZapAi')
@section('page-title', 'Fila de Atendimento')

@section('content')
    @include('partials.flash')

    <p class="mb-6 text-sm text-slate-600">
        Clientes qualificados aguardando contato humano, ordenados por prioridade.
    </p>

    @if ($handoffs->isEmpty())
        <div class="rounded-xl border border-slate-200 bg-white p-8 text-center text-slate-500 shadow-sm">
            Nenhum cliente aguardando atendimento no momento.
        </div>
    @else
        <div class="space-y-4">
            @foreach ($handoffs as $handoff)
                <article class="rounded-xl border border-slate-200 bg-white p-6 shadow-sm">
                    <div class="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
                        <div class="min-w-0 flex-1">
                            <div class="flex flex-wrap items-center gap-2">
                                <h2 class="text-lg font-semibold text-slate-900">
                                    {{ $handoff->contact?->display_name ?: $handoff->contact?->phone ?: 'Cliente' }}
                                </h2>
                                <span class="rounded-full bg-amber-100 px-2.5 py-0.5 text-xs font-medium text-amber-800">
                                    Prioridade {{ $handoff->priority_score }}
                                </span>
                                @if ($handoff->leadScore?->requested_visit)
                                    <span class="rounded-full bg-emerald-100 px-2.5 py-0.5 text-xs font-medium text-emerald-800">
                                        Solicitou visita
                                    </span>
                                @endif
                            </div>

                            <p class="mt-1 text-sm text-slate-500">
                                Aguardando há {{ $handoff->queued_at?->diffForHumans(short: true) }}
                                @if ($handoff->leadScore)
                                    · {{ $handoff->leadScore->properties_viewed }} imóvel(is) visto(s)
                                @endif
                            </p>

                            <pre class="mt-4 max-h-64 overflow-auto whitespace-pre-wrap rounded-lg bg-slate-50 p-4 text-sm text-slate-700">{{ $handoff->summary?->body }}</pre>
                        </div>

                        <form method="POST" action="{{ route('handoff.claim', $handoff) }}" class="shrink-0">
                            @csrf
                            <button
                                type="submit"
                                class="inline-flex w-full items-center justify-center rounded-lg bg-emerald-600 px-4 py-2.5 text-sm font-medium text-white transition hover:bg-emerald-700 lg:w-auto"
                            >
                                Assumir atendimento
                            </button>
                        </form>
                    </div>
                </article>
            @endforeach
        </div>
    @endif
@endsection
