<?php

/**
 * Guest List Import Preview and Validation Report View
 * Location: app/views/guest/import_preview.php
 *
 * Variables expected:
 *   $title     — string — Page title.
 *   $report    — array — Detailed status array for each spreadsheet row.
 *   $summary   — array — Counters of parsed rows states.
 *   $has_valid — bool — True if there are valid rows to import.
 *
 * @package Inveetaire
 * @ref     RC-012
 */
$role = \App\Core\Session::get('role_snapshot') === 'crew' ? 'crew' : 'couple';
/** @var string $title */
/** @var array $report */
/** @var array $summary */
/** @var bool $has_valid */

?>

<div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 space-y-8 py-4">

    <!-- Header Section -->
    <div class="border-b border-slate-200 pb-5 mb-6">
        <a href="<?= url("/app/{$role}/guests/import") ?>" class="text-[10px] font-bold uppercase tracking-wider link-elegant flex items-center gap-1.5 mb-2">
            ◀ Back to Upload Form
        </a>
        <h1 class="text-2xl font-black text-slate-800 tracking-tight">Import Preview &amp; Validation Report</h1>
        <p class="text-xs text-slate-400 mt-1">Review row-level validation warnings and conflicts before final database import.</p>
    </div>

    <!-- ── 1. SUMMARY CARDS ────────────────────────────────────────────── -->
    <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
        <!-- Total -->
        <div class="card p-4 bg-slate-50/50">
            <span class="text-[10px] text-slate-400 uppercase font-bold tracking-wider block">Total Rows</span>
            <span class="text-xl font-bold text-slate-800 mt-1 block font-mono"><?= $summary['total_rows'] ?></span>
        </div>

        <!-- Valid -->
        <div class="card p-4 bg-green-50/30 border-green-100">
            <span class="text-[10px] text-green-600 uppercase font-bold tracking-wider block">Valid Rows</span>
            <span class="text-xl font-bold text-green-700 mt-1 block font-mono"><?= $summary['valid'] ?></span>
        </div>

        <!-- Duplicates -->
        <div class="card p-4 bg-yellow-50/30 border-yellow-100">
            <span class="text-[10px] text-yellow-600 uppercase font-bold tracking-wider block">Duplicate Rows</span>
            <span class="text-xl font-bold text-yellow-700 mt-1 block font-mono"><?= $summary['duplicates'] ?></span>
        </div>

        <!-- Invalid -->
        <div class="card p-4 bg-red-50/30 border-red-100">
            <span class="text-[10px] text-red-500 uppercase font-bold tracking-wider block">Invalid Rows</span>
            <span class="text-xl font-bold text-red-700 mt-1 block font-mono"><?= $summary['invalid'] ?></span>
        </div>
    </div>

    <!-- ── 2. PREVIEW ACTION CONSOLE ───────────────────────────────────── -->
    <div class="card p-5 border-l-4 <?= $has_valid ? 'border-l-purple-500 bg-purple-50/10' : 'border-l-red-500 bg-red-50/10' ?> flex flex-col md:flex-row justify-between items-center gap-4">
        <div class="space-y-1 text-center md:text-left">
            <h3 class="text-sm font-bold text-slate-800">
                <?= $has_valid ? 'Ready to finalize import!' : 'No valid guest records found.' ?>
            </h3>
            <p class="text-xs text-slate-500 leading-normal max-w-lg">
                <?php if ($has_valid): ?>
                    Clicking <strong>Confirm Import</strong> will save the <?= $summary['valid'] ?> valid rows into your workspace guest registry. Duplicates and invalid rows will be skipped.
                <?php else: ?>
                    Please review the validation errors table below. Correct any duplicate or invalid columns in your Excel file and upload again.
                <?php endif; ?>
            </p>
        </div>
        
        <div class="flex gap-3">
            <a href="<?= url("/app/{$role}/guests/import") ?>" class="btn btn-secondary text-xs font-semibold px-5 py-2.5">
                Cancel
            </a>
            <?php if ($has_valid): ?>
                <form action="<?= url("/app/{$role}/guests/import/confirm") ?>" method="POST">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-primary text-xs font-semibold px-5 py-2.5">
                        🚀 Confirm Import (<?= $summary['valid'] ?> Valid)
                    </button>
                </form>
            <?php endif; ?>
        </div>
    </div>

    <!-- ── 3. DETAILED ROW REPORT TABLE ────────────────────────────────── -->
    <div class="card p-0 overflow-hidden shadow-sm">
        <div class="px-6 py-4 border-b border-slate-100 bg-slate-50/50">
            <h3 class="text-sm font-bold text-slate-800">Row-by-Row Spreadsheet Report</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-left border-collapse text-xs">
                <thead>
                    <tr class="bg-slate-50 border-b border-slate-100 font-bold text-slate-600 uppercase tracking-wider">
                        <th class="px-6 py-4 text-center" style="width: 80px;">Row</th>
                        <th class="px-6 py-4">Guest Name</th>
                        <th class="px-6 py-4">Phone Number</th>
                        <th class="px-6 py-4" style="width: 150px;">Validation Status</th>
                        <th class="px-6 py-4">Reason / Notes</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 text-slate-700">
                    <?php foreach ($report as $r): ?>
                        <tr class="hover:bg-slate-50/30 transition-colors">
                            <td class="px-6 py-4 text-center font-mono font-semibold text-slate-500">
                                <?= $r['row'] ?>
                            </td>
                            <td class="px-6 py-4 font-semibold text-slate-800">
                                <?= escape_html($r['name']) ?>
                            </td>
                            <td class="px-6 py-4 font-mono text-slate-500">
                                <?= escape_html($r['phone']) ?>
                            </td>
                            <td class="px-6 py-4">
                                <?php if ($r['status'] === 'valid'): ?>
                                    <span class="inline-block bg-green-50 text-green-700 border border-green-100 px-2.5 py-0.5 rounded text-[10px] font-bold">
                                        Valid
                                    </span>
                                <?php elseif ($r['status'] === 'duplicate'): ?>
                                    <span class="inline-block bg-yellow-50 text-yellow-700 border border-yellow-100 px-2.5 py-0.5 rounded text-[10px] font-bold">
                                        Duplicate
                                    </span>
                                <?php else: ?>
                                    <span class="inline-block bg-red-50 text-red-700 border border-red-100 px-2.5 py-0.5 rounded text-[10px] font-bold">
                                        Invalid
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4 <?= ($r['status'] !== 'valid') ? 'text-red-500 font-semibold' : 'text-slate-400' ?>">
                                <?= $r['reason'] ? escape_html($r['reason']) : '<span class="text-slate-300 italic">No warnings</span>' ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>
