<?php

/**
 * Guest List Import Result View
 *
 * Renders the import metrics summary card and skipped row error log.
 *
 * Variables expected:
 *   $stats — array — Summary details returned by GuestService::importGuests().
 *            Contains keys: 'total_rows', 'imported', 'skipped', 'duplicate', 'errors', 'time_ms'.
 *
 * @package Inveetaire
 * @ref     PILOT-002
 */
$role = \App\Core\Session::get('role_snapshot') === 'crew' ? 'crew' : 'couple';
/** @var array $stats */
$stats = $stats ?? [
    'total_rows'  => 0,
    'imported'    => 0,
    'skipped'     => 0,
    'duplicate'   => 0,
    'errors'      => [],
    'time_ms'     => 0
];

$total     = (int) $stats['total_rows'];
$imported  = (int) $stats['imported'];
$skipped   = (int) $stats['skipped'];
$duplicate = (int) $stats['duplicate'];
$errors    = $stats['errors'] ?? [];
$duration  = (int) $stats['time_ms'];

?>

<div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
    
    <!-- Title Section -->
    <div class="border-b border-slate-200 pb-5 mb-8 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-3xl font-extrabold tracking-tight text-slate-900">Import Summary</h1>
            <p class="text-sm text-slate-500 mt-1">Review the guest list processing metrics below.</p>
        </div>
        <div>
            <a href="<?= url("/app/{$role}/guests/import") ?>" class="btn btn-secondary btn-md">
                🔄 Import Another File
            </a>
        </div>
    </div>

    <!-- Summary Metrics Cards Grid -->
    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
        
        <!-- Total Rows Card -->
        <div class="card p-5 text-center">
            <p class="text-xs font-semibold text-slate-400 uppercase tracking-wider">Total Rows</p>
            <p class="text-3xl font-bold text-slate-800 mt-2 font-mono"><?= $total ?></p>
        </div>

        <!-- Success Imported Card -->
        <div class="card bg-green-50/50 border-green-200/60 p-5 text-center">
            <p class="text-xs font-semibold text-green-600 uppercase tracking-wider">Imported</p>
            <p class="text-3xl font-bold text-green-700 mt-2 font-mono"><?= $imported ?></p>
        </div>

        <!-- Duplicates Skipped Card -->
        <div class="card bg-amber-50/50 border-amber-200/60 p-5 text-center">
            <p class="text-xs font-semibold text-amber-600 uppercase tracking-wider">Duplicates</p>
            <p class="text-3xl font-bold text-amber-700 mt-2 font-mono"><?= $duplicate ?></p>
        </div>

        <!-- Skipped Errors Card -->
        <div class="card bg-red-50/50 border-red-200/60 p-5 text-center">
            <p class="text-xs font-semibold text-red-600 uppercase tracking-wider">Errors / Skipped</p>
            <p class="text-3xl font-bold text-red-700 mt-2 font-mono"><?= count($errors) ?></p>
        </div>

    </div>

    <!-- Additional info banner -->
    <div class="bg-slate-800 text-white rounded-xl px-5 py-3 mb-8 flex items-center justify-between text-xs shadow-sm">
        <span>Completed processing in <strong><?= $duration ?> ms</strong></span>
        <span>Secure guest tokens generated successfully.</span>
    </div>

    <!-- Skipped Rows Detail Log -->
    <?php if (!empty($errors)): ?>
        <div class="card p-0 overflow-hidden mb-8">
            <div class="px-6 py-4 border-b border-slate-100 bg-slate-50/50">
                <h3 class="text-sm font-bold text-slate-800 uppercase tracking-wider">Skipped Rows log</h3>
                <p class="text-xs text-slate-400 mt-0.5">The following rows contain duplicate data or validation errors and were skipped during import.</p>
            </div>
            
            <div class="overflow-x-auto">
                <table class="table-elegant text-xs">
                    <thead>
                        <tr>
                            <th class="px-6 py-3">Row</th>
                            <th class="px-6 py-3">Guest Name</th>
                            <th class="px-6 py-3">Issue Detail</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 text-slate-600 font-medium">
                        <?php foreach ($errors as $error): ?>
                            <tr>
                                <td class="px-6 py-4.5 font-mono text-slate-400 font-semibold"><?= (int) $error['row'] ?></td>
                                <td class="px-6 py-4.5 font-semibold text-slate-800"><?= escape_html($error['name']) ?></td>
                                <td class="px-6 py-4.5 text-red-600"><?= escape_html($error['reason']) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    <?php else: ?>
        <div class="alert alert-success p-6 justify-center text-center text-sm font-semibold mb-8">
            🎉 All rows processed and imported successfully with zero errors!
        </div>
    <?php endif; ?>

    <!-- Bottom Actions -->
    <div class="flex justify-center gap-4">
        <?php
        $dashboardUrl = ($role === 'crew') ? '/app/crew/operation/scanner' : '/app/couple/dashboard';
        ?>
        <a href="<?= url($dashboardUrl) ?>" class="btn btn-primary btn-md">
            Go to Dashboard
        </a>
    </div>

</div>
