@extends('layouts.app')

@section('title', 'Import ' . $batch->file_name . ' – Gap Finder Analytics')

@section('content')
    <div class="page-actions">
        <a href="{{ route('imports.index') }}" class="btn btn-ghost">← Back to imports</a>
        <a href="{{ route('imports.mapping', $batch) }}" class="btn btn-primary">Manual mapping</a>
    </div>
    <h1>{{ $batch->file_name }}</h1>
    <p class="muted">Status: {{ $batch->status }} · {{ $batch->stagingRows->count() }} rows</p>

    @if (!empty($stats))
        <div class="grid grid-2 mt-1">
            <div class="card mb-0"><div class="card-title">Valid</div><div class="card-value">{{ $stats['valid'] ?? 0 }}</div></div>
            <div class="card mb-0"><div class="card-title">Partial</div><div class="card-value">{{ $stats['partial'] ?? 0 }}</div></div>
            <div class="card mb-0"><div class="card-title">Invalid</div><div class="card-value">{{ $stats['invalid'] ?? 0 }}</div></div>
        </div>
    @endif

    <div class="card mt-1">
        <h2>Staging rows (first 100)</h2>
        <div class="table-wrap"><table>
            <thead>
                <tr>
                    <th>Row</th>
                    <th>Status</th>
                    <th>Raw data</th>
                    <th>Errors</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($batch->stagingRows->take(100) as $row)
                    <tr>
                        <td>{{ $row->row_index }}</td>
                        <td>{{ $row->validation_status }}</td>
                        <td><pre>{{ json_encode($row->raw_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre></td>
                        <td class="muted">{{ $row->validation_errors ? implode('; ', $row->validation_errors) : '–' }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table></div>
        @if ($batch->stagingRows->count() > 100)
            <p class="muted mt-1">Showing first 100 of {{ $batch->stagingRows->count() }} rows.</p>
        @endif
    </div>
@endsection
