@extends('layouts.app')

@section('title', 'Preview Import')

@section('content')
    @php
        $validCount = $rows->filter->isValid()->count();
        $invalidCount = $rows->count() - $validCount;
    @endphp

    <x-page-header
        title="Preview Import"
        :subtitle="$batch->original_filename">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('imports.index') }}">Imports</a></li>
            <li class="breadcrumb-item active" aria-current="page">Preview</li>
        </x-slot:breadcrumbs>

        <a href="{{ route('imports.create', ['module' => $batch->module]) }}" class="btn btn-outline-secondary">
            <i class="bi bi-x-lg" aria-hidden="true"></i>Cancel
        </a>
    </x-page-header>

    <div class="row g-3 mb-3">
        <div class="col-sm-4">
            <div class="card">
                <div class="card-body">
                    <div class="ams-overline">Total Rows</div>
                    <div class="fs-3 fw-semibold ams-numeric">{{ $rows->count() }}</div>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="card">
                <div class="card-body">
                    <div class="ams-overline">Ready To Import</div>
                    <div class="fs-3 fw-semibold ams-numeric text-success">{{ $validCount }}</div>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="card">
                <div class="card-body">
                    <div class="ams-overline">Will Be Skipped</div>
                    <div class="fs-3 fw-semibold ams-numeric {{ $invalidCount > 0 ? 'text-danger' : '' }}">{{ $invalidCount }}</div>
                </div>
            </div>
        </div>
    </div>

    @if ($validCount === 0)
        <div class="alert alert-danger d-flex align-items-center gap-2 mb-3" role="alert">
            <i class="bi bi-exclamation-triangle" aria-hidden="true"></i>
            <div>Every row has at least one problem, so there is nothing to import yet. Fix the rows below and upload again.</div>
        </div>
    @elseif ($invalidCount > 0)
        <div class="alert alert-warning d-flex align-items-center gap-2 mb-3" role="alert">
            <i class="bi bi-exclamation-triangle" aria-hidden="true"></i>
            <div>{{ $invalidCount }} {{ Str::plural('row', $invalidCount) }} will be skipped and reported — the other {{ $validCount }} can still be imported now.</div>
        </div>
    @endif

    @if ($documentMatches)
        @if ($documentMatches->rejected !== [] || $documentMatches->unmatched !== [])
            <div class="alert alert-warning mb-3" role="alert">
                <div class="d-flex align-items-center gap-2 mb-2">
                    <i class="bi bi-exclamation-triangle" aria-hidden="true"></i>
                    <div>Some uploaded documents will not be attached:</div>
                </div>
                <ul class="mb-0 ps-4 ams-text-xs">
                    @foreach ($documentMatches->unmatched as $entry)
                        <li><strong>{{ $entry['original_name'] }}</strong> — no asset with tag "{{ $entry['normalized_tag'] }}" in this file.</li>
                    @endforeach
                    @foreach ($documentMatches->rejected as $entry)
                        <li><strong>{{ $entry['original_name'] }}</strong> — {{ $entry['rejected_reason'] }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
    @endif

    <x-table.wrapper>
        <x-slot:head>
            <tr>
                <th scope="col" style="width: 3.5rem;">Row</th>
                @foreach ($previewColumns as [$key, $label])
                    <th scope="col">{{ $label }}</th>
                @endforeach
                <th scope="col">Status</th>
                @if ($documentMatches)
                    <th scope="col">Document</th>
                @endif
                <th scope="col">Details</th>
            </tr>
        </x-slot:head>

        @foreach ($rows as $row)
            <tr class="{{ $row->isValid() ? '' : 'table-danger' }}">
                <td class="ams-cell-secondary ams-numeric">{{ $row->rowNumber }}</td>
                @foreach ($previewColumns as $index => [$key, $label])
                    <td class="{{ $index === 0 ? 'ams-cell-primary' : 'ams-cell-secondary' }}">{{ $row->raw[$key] ?? '—' }}</td>
                @endforeach
                <td>
                    @if ($row->isValid())
                        <x-badge color="success" icon="check-circle">Ready</x-badge>
                    @else
                        <x-badge color="danger" icon="x-circle">Error</x-badge>
                    @endif
                </td>
                @if ($documentMatches)
                    <td>
                        @if ($row->isValid())
                            @php $tag = mb_strtoupper(trim((string) ($row->data['asset_tag'] ?? ''))); @endphp
                            @if (isset($documentMatches->matched[$tag]))
                                <x-badge color="success" icon="check-circle">Found</x-badge>
                            @else
                                <x-badge color="warning" icon="exclamation-triangle">Missing</x-badge>
                            @endif
                        @else
                            <span class="text-secondary">—</span>
                        @endif
                    </td>
                @endif
                <td class="ams-text-xs">
                    @if (! $row->isValid())
                        <ul class="mb-0 ps-3">
                            @foreach ($row->errors as $error)
                                <li class="text-danger">{{ $error }}</li>
                            @endforeach
                        </ul>
                    @endif
                </td>
            </tr>
        @endforeach

        <x-slot:footer>
            <div class="ams-table-count">
                {{ trans_choice(':count row|:count rows', $rows->count(), ['count' => $rows->count()]) }},
                rows with a problem listed first.
            </div>
        </x-slot:footer>
    </x-table.wrapper>

    <div class="d-flex justify-content-end gap-2 mt-3">
        <a href="{{ route('imports.create', ['module' => $batch->module]) }}" class="btn btn-outline-secondary">Cancel</a>

        <form method="POST" action="{{ route('imports.confirm', $batch) }}" data-ams-progress="Importing…">
            @csrf
            <button type="submit" class="btn btn-primary" @disabled($validCount === 0)>
                <i class="bi bi-check-lg" aria-hidden="true"></i>
                Import {{ $validCount }} {{ Str::plural(Str::singular($entityLabel), $validCount) }}
            </button>
        </form>
    </div>
@endsection
