<?php

/**
 * Event-Specific Guest Check-In Details View
 * Location: app/views/operation/guest.php
 *
 * Variables expected:
 *   $title            — string — Page title.
 *   $guest            — array — Resolved guest row.
 *   $event            — array — Current wedding event context.
 *   $allowed_events   — array — Events the guest is explicitly invited to.
 *   $is_invited       — bool — True if guest is invited to the current event.
 *   $checkin_record   — array|null — Current checkin record if exists.
 *   $has_wish         — bool — True if guest has left a congratulations wish.
 *   $errors           — array — List of errors.
 *   $success          — string|null — Success flash message.
 *
 * @package Inveetaire
 * @ref     RC-013
 */

$role = \App\Core\Session::get('role_snapshot') === 'crew' ? 'crew' : 'couple';

?>

<div class="max-w-xl mx-auto space-y-6 py-4" x-data="{ rejectModalOpen: false, overrideModalOpen: false }">
    
    <!-- Navigation back header -->
    <div class="flex items-center justify-between">
        <a href="<?= url("/app/{$role}/operation/event/{$event['id']}/checkin") ?>" class="link-elegant text-xs font-semibold flex items-center gap-1">
            <i class="fa-solid fa-arrow-left text-[10px]"></i> Back to Check-in Scanner
        </a>
        <span class="text-xxs font-bold text-slate-400 uppercase tracking-widest bg-slate-100 px-3 py-1 rounded-full">
            <?= escape_html($event['event_name']) ?>
        </span>
    </div>

    <!-- Session Alerts -->
    <?php if (!empty($errors)): ?>
        <div class="rounded-2xl bg-red-50 border border-red-200 p-4 text-xs text-red-700 shadow-sm">
            <ul class="list-disc pl-4 space-y-1">
                <?php foreach ($errors as $err): ?>
                    <li><?= escape_html($err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <?php if ($success !== null): ?>
        <div class="rounded-2xl bg-emerald-50 border border-emerald-200 p-4 text-xs text-emerald-800 font-bold text-center shadow-sm">
            <?= escape_html($success) ?>
        </div>
    <?php endif; ?>

    <!-- ── GUEST IDENTIFICATION PROFILE ────────────────────────────────── -->
    <div class="card p-0 overflow-hidden shadow-xl border border-slate-200">
        
        <!-- Header status -->
        <div class="p-6 border-b border-slate-100 bg-slate-50/50 flex justify-between items-center">
            <div>
                <span class="text-[9px] uppercase tracking-widest text-slate-400 font-black block">Guest Profile Code: <?= escape_html($guest['guest_code']) ?></span>
                <h2 class="text-xl font-black text-slate-800 mt-1"><?= escape_html($guest['full_name']) ?></h2>
            </div>
            <div>
                <?php if ($checkin_record !== null): ?>
                    <span class="inline-block bg-green-100 text-green-800 border border-green-200 px-3 py-1 rounded-xl text-xxs font-black tracking-wide uppercase">
                        ✓ Checked In
                    </span>
                <?php else: ?>
                    <span class="inline-block bg-slate-100 text-slate-500 border border-slate-200 px-3 py-1 rounded-xl text-xxs font-black tracking-wide uppercase">
                        ⌛ Not Checked In
                    </span>
                <?php endif; ?>
            </div>
        </div>

        <div class="p-6 space-y-6">
            
            <!-- Metadata Attributes Grid -->
            <div class="grid grid-cols-2 gap-4 text-xs border-b border-slate-100 pb-5">
                <div>
                    <span class="text-slate-400 font-bold block uppercase text-[10px]">Guest Group Category</span>
                    <span class="text-slate-800 font-black mt-1 inline-block uppercase text-xxs bg-slate-100 px-2.5 py-0.5 rounded-full">
                        <?= escape_html($guest['guest_group'] ?? 'others') ?>
                    </span>
                </div>
                <div>
                    <span class="text-slate-400 font-bold block uppercase text-[10px]">Pax Capacity (Seats)</span>
                    <span class="text-slate-800 font-black mt-1 inline-block font-mono text-sm">
                        <?= (int) ($guest['invited_pax'] ?? 1) ?> seats
                    </span>
                </div>
                <div>
                    <span class="text-slate-400 font-bold block uppercase text-[10px]">Guest Side</span>
                    <span class="text-slate-800 font-black mt-1 inline-block uppercase text-xxs bg-slate-100 px-2.5 py-0.5 rounded-full">
                        <?= escape_html($guest['side'] ?? 'couple') ?>'s side
                    </span>
                </div>
                <div>
                    <span class="text-slate-400 font-bold block uppercase text-[10px]">RSVP Status</span>
                    <span class="text-slate-800 font-black mt-1 inline-block uppercase text-xxs bg-slate-100 px-2.5 py-0.5 rounded-full">
                        <?= escape_html($guest['rsvp_status'] ?? 'pending') ?>
                    </span>
                </div>
            </div>

            <!-- Allowed Events list -->
            <div class="space-y-2 border-b border-slate-100 pb-5">
                <label class="text-slate-400 font-bold block uppercase text-[10px] tracking-wider">Invitation Allowed Ceremonies</label>
                <div class="flex flex-wrap gap-1.5 mt-1.5">
                    <?php if (empty($allowed_events)): ?>
                        <span class="text-red-500 font-bold text-xxs bg-red-50 border border-red-200 px-2.5 py-1 rounded-full uppercase flex items-center gap-1">
                            <i class="fa-solid fa-ban text-[10px]"></i> Invited to 0 Events
                        </span>
                    <?php else: ?>
                        <?php foreach ($allowed_events as $ae): 
                            $isCurrent = ((int) $ae['id'] === (int) $event['id']);
                        ?>
                             <span class="px-2.5 py-1 rounded-full text-xxs font-bold border flex items-center gap-1 <?= $isCurrent ? 'bg-blue-100 text-blue-700 border-blue-200 font-black' : 'bg-slate-50 text-slate-500 border-slate-200' ?>">
                                <?= escape_html($ae['event_name']) ?> <?= $isCurrent ? '<i class="fa-solid fa-circle-check text-[10px]"></i>' : '' ?>
                            </span>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Wish status -->
            <?php if ($has_wish): ?>
                <div class="rounded-xl p-3 text-xxs font-semibold flex items-center gap-2" style="background-color: var(--brand-rose-gold-lt); border: 1px solid var(--brand-rose-gold); color: var(--brand-charcoal);">
                    <i class="fa-solid fa-circle-check" style="color: var(--brand-charcoal);"></i>
                    <span>Guest has left congratulations wishes.</span>
                </div>
            <?php endif; ?>

            <!-- ── CHECK-IN WORKFLOW HANDLERS ─────────────────────────────── -->
            
            <?php if ($checkin_record !== null): ?>
                <!-- ALREADY CHECKED-IN STATE -->
                <div class="bg-green-50 border border-green-200 rounded-2xl p-5 space-y-3">
                    <h3 class="text-xs font-black text-green-800 uppercase tracking-wider flex items-center gap-1.5">
                        <i class="fa-solid fa-circle-check"></i> Checked In Successfully
                    </h3>
                    <div class="text-xxs text-green-700 space-y-1 leading-relaxed">
                        <p><strong>Arrived At:</strong> <?= escape_html($checkin_record['checked_in_at']) ?></p>
                        <p><strong>Operator:</strong> <?= escape_html($checkin_record['operator_name'] ?? 'System') ?></p>
                        <p><strong>Method:</strong> <?= strtoupper(escape_html($checkin_record['checkin_method'])) ?></p>
                        <?php if (!empty($checkin_record['notes'])): ?>
                            <p class="mt-1 bg-white/60 p-2 rounded border border-green-100">
                                <strong>Notes:</strong> <?= escape_html($checkin_record['notes']) ?>
                            </p>
                        <?php endif; ?>
                    </div>
                </div>

            <?php else: ?>
                <!-- NOT CHECKED-IN STATE -->
                
                <?php if ($is_invited): ?>
                    <!-- ALLOWED CHECK-IN FORM -->
                    <div class="bg-blue-50 border border-blue-200 rounded-2xl p-5 space-y-4">
                        <div>
                            <h3 class="text-xs font-black text-blue-800 uppercase tracking-wider flex items-center gap-1.5">
                                <i class="fa-solid fa-qrcode"></i> Confirm Arrival Check-In
                            </h3>
                            <p class="text-[10px] text-blue-600 mt-0.5">This guest is explicitly invited to <?= escape_html($event['event_name']) ?>.</p>
                        </div>

                        <form action="<?= url("/app/{$role}/operation/event/{$event['id']}/guest/{$guest['guest_code']}/checkin") ?>" method="POST" class="space-y-3">
                            <?= csrf_field() ?>
                            
                            <div class="form-group">
                                <label for="checkin_notes" class="text-[10px] uppercase font-bold text-slate-400 block mb-1">Check-in Notes (Optional)</label>
                                <input 
                                    type="text" 
                                    id="checkin_notes" 
                                    name="checkin_notes" 
                                    placeholder="e.g. Gift box received, Table 12, etc." 
                                    class="form-input text-xs w-full bg-white rounded-xl"
                                    autocomplete="off"
                                >
                            </div>

                             <button type="submit" class="btn btn-primary bg-blue-600 border-blue-600 hover:bg-blue-700 text-xs font-bold w-full py-3 rounded-xl shadow-md text-white flex items-center justify-center gap-1.5">
                                <i class="fa-solid fa-circle-check"></i> Confirm Check-In
                            </button>
                        </form>
                    </div>

                <?php else: ?>
                    <!-- NOT INVITED WARNING - REQUIRE MANUAL OVERRIDE -->
                    <div class="bg-orange-50 border border-orange-200 rounded-2xl p-5 space-y-5">
                        <div class="space-y-1">
                            <h3 class="text-xs font-black text-orange-800 uppercase tracking-wider flex items-center gap-1.5">
                                <i class="fa-solid fa-triangle-exclamation"></i> Entry Restriction Warning
                            </h3>
                            <p class="text-[10px] text-orange-700 leading-relaxed font-semibold">
                                This guest is NOT explicitly invited to <strong><?= escape_html($event['event_name']) ?></strong>.
                            </p>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            
                            <!-- Option A: Reject -->
                            <div class="card bg-white p-4 border border-slate-200 rounded-xl flex flex-col justify-between">
                                <div class="text-[9px] text-slate-400 leading-relaxed mb-3">
                                    Deny entry to this ceremony. This action will log a check-in rejection event in the audit trail.
                                </div>
                                <button type="button" @click="rejectModalOpen = true" class="btn btn-secondary border-red-500 text-red-600 hover:bg-red-50 text-[10px] font-black w-full py-2.5 rounded-lg flex items-center justify-center gap-1">
                                    <i class="fa-solid fa-ban"></i> Reject Check-In
                                </button>
                            </div>

                            <!-- Option B: Manual Override -->
                            <div class="card bg-white p-4 border border-slate-200 rounded-xl space-y-3">
                                <div class="text-[9px] text-slate-400 leading-relaxed">
                                    Manually force check-in. This logs a manual override event requiring audit parameters.
                                </div>
                                <form id="override-form" action="<?= url("/app/{$role}/operation/event/{$event['id']}/guest/{$guest['guest_code']}/checkin") ?>" method="POST" @submit.prevent="if ($el.checkValidity()) overrideModalOpen = true" class="space-y-2">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="is_override" value="1">
                                    
                                    <div>
                                        <label for="override_reason" class="text-[9px] text-slate-400 font-bold block mb-1">Reason</label>
                                        <select id="override_reason" name="override_reason" class="form-select text-[10px] py-1 px-2 w-full rounded-md" required>
                                            <option value="">Select Reason...</option>
                                            <option value="VIP">VIP Request</option>
                                            <option value="Family Request">Family Request</option>
                                            <option value="Organizer Approval">Organizer Approval</option>
                                            <option value="Extra Pax">Extra Pax</option>
                                            <option value="Other">Other Approval</option>
                                        </select>
                                    </div>

                                    <div>
                                        <label for="override_notes" class="text-[9px] text-slate-400 font-bold block mb-1">Notes</label>
                                        <input 
                                            type="text" 
                                            id="override_notes" 
                                            name="override_notes" 
                                            placeholder="Specify details..." 
                                            class="form-input text-[10px] py-1 px-2 w-full rounded-md"
                                        >
                                    </div>
                                    <div class="flex gap-3 mt-2">
                                        <a href="<?= url("/app/{$role}/operation/event/{$event['id']}") ?>" class="btn btn-secondary bg-slate-100 hover:bg-slate-200 text-slate-600 text-[10px] font-black flex-[1] py-2.5 rounded-lg flex items-center justify-center">
                                            Cancel
                                        </a>
                                        <button type="submit" class="btn btn-primary text-[10px] font-black flex-[2] py-2.5 rounded-lg flex items-center justify-center gap-3.5" :disabled="isPrinting">
                                            <i class="fa-solid" :class="isPrinting ? 'fa-spinner fa-spin' : 'fa-user-shield'"></i> 
                                            <span x-text="isPrinting ? 'Printing...' : 'Override Check-in'"></span>
                                        </button>
                                    </div>
                                </form>
                            </div>

                        </div>
                    </div>
                <?php endif; ?>

            <?php endif; ?>

        </div>
    </div>

    <!-- ── GUEST JOURNEY TIMELINE (RC-014) ────────────────────────────────── -->
    <?php require ROOT_PATH . '/app/views/engagement/_timeline.php'; ?>

    <!-- ── STANDARD DIALOG MODALS (RC-015) ────────────────────────────── -->
    <!-- Rejection Confirmation Modal -->
    <div 
        class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4" 
        style="display: none;" 
        x-show="rejectModalOpen"
        @keydown.escape.window="rejectModalOpen = false"
        @click.self="rejectModalOpen = false"
        x-transition:enter="transition ease-out duration-200"
        x-transition:enter-start="opacity-0 scale-95"
        x-transition:enter-end="opacity-100 scale-100"
        x-transition:leave="transition ease-in duration-150"
        x-transition:leave-start="opacity-100 scale-100"
        x-transition:leave-end="opacity-0 scale-95"
        role="dialog"
        aria-modal="true"
        aria-labelledby="modal-title-reject"
    >
        <div class="bg-white rounded-2xl max-w-sm w-full p-6 shadow-2xl space-y-6">
            <div class="text-center space-y-2">
                <i class="fa-solid fa-ban text-red-500 text-4xl block"></i>
                <h3 id="modal-title-reject" class="text-sm font-bold text-slate-800">Reject Guest Entry?</h3>
                <p class="text-xs text-slate-500 leading-relaxed">This will deny check-in to this guest and log an entry rejection event in the audit logs.</p>
            </div>
            
            <form action="<?= url("/app/{$role}/operation/event/{$event['id']}/guest/{$guest['guest_code']}/reject") ?>" method="POST" class="flex gap-3 m-0">
                <?= csrf_field() ?>
                <button type="button" @click="rejectModalOpen = false" class="btn btn-secondary flex-1 py-2 text-xs font-bold">
                    Cancel
                </button>
                <button type="submit" class="btn btn-danger flex-1 py-2 text-xs font-bold text-white">
                    Reject Entry
                </button>
            </form>
        </div>
    </div>

    <!-- Manual Override Confirmation Modal -->
    <div 
        class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4" 
        style="display: none;" 
        x-show="overrideModalOpen"
        @keydown.escape.window="overrideModalOpen = false"
        @click.self="overrideModalOpen = false"
        x-transition:enter="transition ease-out duration-200"
        x-transition:enter-start="opacity-0 scale-95"
        x-transition:enter-end="opacity-100 scale-100"
        x-transition:leave="transition ease-in duration-150"
        x-transition:leave-start="opacity-100 scale-100"
        x-transition:leave-end="opacity-0 scale-95"
        role="dialog"
        aria-modal="true"
        aria-labelledby="modal-title-override"
    >
        <div class="bg-white rounded-2xl max-w-sm w-full p-6 shadow-2xl space-y-6">
            <div class="text-center space-y-2">
                <i class="fa-solid fa-triangle-exclamation text-amber-500 text-4xl block"></i>
                <h3 id="modal-title-override" class="text-sm font-bold text-slate-800">Confirm Manual Override?</h3>
                <p class="text-xs text-slate-500 leading-relaxed">This will force arrival check-in for this uninvited guest and create an audit log track. Proceed?</p>
            </div>
            
            <div class="flex gap-3">
                <button type="button" @click="overrideModalOpen = false" class="btn btn-secondary flex-1 py-2 text-xs font-bold">
                    Cancel
                </button>
                <button type="button" @click="document.getElementById('override-form').submit()" class="btn btn-primary bg-blue-600 hover:bg-blue-700 text-white flex-1 py-2 text-xs font-bold">
                    Confirm Override
                </button>
            </div>
        </div>
    </div>

</div>
