@extends('layouts.app')

@section('title', 'Importação — ImóvelZapAi')
@section('page-title', 'Assistente de importação')

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

    <div class="mb-4 flex flex-wrap items-center justify-between gap-3">
        <div>
            <p class="text-sm text-slate-500">Status: <span class="font-semibold text-slate-800">{{ $job->status->value ?? $job->status }}</span></p>
            <p class="text-sm text-slate-500">Arquivo: {{ $job->file?->original_name }}</p>
        </div>
        <a href="{{ route('importing.index') }}" class="text-sm text-emerald-700 hover:underline">Voltar ao painel</a>
    </div>

    @if ($job->preview_summary)
        <div class="mb-6 grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                <p class="text-xs uppercase text-slate-500">Total</p>
                <p class="mt-1 text-2xl font-semibold">{{ $job->preview_summary['total'] ?? 0 }}</p>
            </div>
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                <p class="text-xs uppercase text-slate-500">Válidos</p>
                <p class="mt-1 text-2xl font-semibold">{{ $job->preview_summary['valid'] ?? 0 }}</p>
            </div>
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                <p class="text-xs uppercase text-slate-500">Com erro</p>
                <p class="mt-1 text-2xl font-semibold">{{ $job->preview_summary['with_errors'] ?? 0 }}</p>
            </div>
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                <p class="text-xs uppercase text-slate-500">Fotos ausentes</p>
                <p class="mt-1 text-2xl font-semibold">{{ $job->preview_summary['missing_photos'] ?? 0 }}</p>
            </div>
        </div>
    @endif

    @if (in_array(($job->status->value ?? $job->status), ['preview_ready', 'awaiting_mapping', 'ready'], true))
        <div class="mb-6 rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
            <h2 class="mb-3 text-sm font-semibold text-slate-800">Corrigir inconsistências / mapeamento</h2>
            <form method="POST" action="{{ route('importing.mapping', $job) }}" class="space-y-3">
                @csrf
                @method('PATCH')
                @foreach (($job->preview_summary['ambiguities'] ?? []) as $item)
                    @php
                        $bucket = match ($item['field'] ?? '') {
                            'property_type' => 'property_types',
                            'purpose' => 'purposes',
                            'state' => 'states',
                            'city' => 'cities',
                            default => null,
                        };
                    @endphp
                    @if ($bucket)
                        <label class="block text-sm">
                            <span class="mb-1 block font-medium text-slate-700">
                                {{ $item['field'] }}: {{ $item['raw'] }}
                            </span>
                            <input type="text" name="{{ $bucket }}[{{ $item['key'] }}]"
                                class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"
                                placeholder="Valor de destino (enum/UUID)">
                        </label>
                    @endif
                @endforeach
                <button type="submit" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">
                    Salvar mapeamento
                </button>
            </form>

            <form method="POST" action="{{ route('importing.confirm', $job) }}" class="mt-4">
                @csrf
                <button type="submit" class="rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-500">
                    Confirmar importação
                </button>
            </form>
        </div>
    @endif

    @if (in_array(($job->status->value ?? $job->status), ['queued', 'processing', 'analyzing'], true))
        <div class="mb-6 rounded-xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900">
            Processando em segundo plano…
            @if ($job->progress_total)
                {{ $job->progress_current }} / {{ $job->progress_total }}
            @endif
        </div>
    @endif

    @if (in_array(($job->status->value ?? $job->status), ['completed', 'completed_with_errors', 'failed'], true))
        <div class="mb-6 flex flex-wrap gap-3">
            <a href="{{ route('importing.report', $job) }}" class="rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-500">
                Ver relatório
            </a>
            @if (in_array(($job->status->value ?? $job->status), ['completed_with_errors', 'failed'], true))
                <form method="POST" action="{{ route('importing.retry', $job) }}">
                    @csrf
                    <button type="submit" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">
                        Tentar novamente
                    </button>
                </form>
            @endif
        </div>
    @endif

    <div class="overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">
        <table class="min-w-full divide-y divide-slate-200 text-sm">
            <thead class="bg-slate-50 text-left text-xs font-semibold uppercase text-slate-500">
                <tr>
                    <th class="px-4 py-3">Severidade</th>
                    <th class="px-4 py-3">Código</th>
                    <th class="px-4 py-3">Listing</th>
                    <th class="px-4 py-3">Mensagem</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                @forelse ($job->errors as $error)
                    <tr>
                        <td class="px-4 py-3">{{ $error->severity->value ?? $error->severity }}</td>
                        <td class="px-4 py-3">{{ $error->code }}</td>
                        <td class="px-4 py-3">{{ $error->external_id ?? $error->listing_index }}</td>
                        <td class="px-4 py-3">{{ $error->message }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="4" class="px-4 py-6 text-center text-slate-500">Nenhuma inconsistência registrada.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
@endsection
