<?php

/**
 * Add Guest Manual Form View
 * Location: app/views/guest/create.php
 *
 * Variables expected:
 *   $title  — string — Page title.
 *   $errors — array — Form field validation errors.
 *   $old    — array — Old form values for sticky inputs.
 *
 * @package Inveetaire
 * @ref     RC-012
 */
$role = \App\Core\Session::get('role_snapshot') === 'crew' ? 'crew' : 'couple';
/** @var string $title */
/** @var array $errors */
/** @var array $old */

$errors = $errors ?? [];
$old = $old ?? [];

?>

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

    <!-- Header Section -->
    <div class="mb-6">
        <div class="page-breadcrumb">
            <a href="<?= url("/app/{$role}/guests") ?>">Guest List</a>
            <span class="sep">/</span>
            <span>Add Guest</span>
        </div>
        <h1 class="page-h1">
            <span class="page-header-icon">
                <i class="fa-solid fa-user-plus"></i>
            </span>
            Add New Guest
        </h1>
        <p class="page-subtitle">Manually add a guest to your wedding guest list.</p>
    </div>

    <!-- Card Form -->
    <div class="card p-6">
        <form action="<?= url("/app/{$role}/guests/create") ?>" method="POST" class="space-y-5">
            <?= csrf_field() ?>

            <!-- Honorific / Prefix (G006) -->
            <div class="form-group">
                <label for="honorific" class="form-label">Honorific / Prefix (Optional)</label>
                <input 
                    type="text" 
                    id="honorific" 
                    name="honorific" 
                    value="<?= escape_html($old['honorific'] ?? '') ?>" 
                    placeholder="e.g. Mr., Mrs., Family of, Yth. Bapak/Ibu" 
                    class="form-input text-xs w-full mt-1.5" 
                >
                <?php if (isset($errors['honorific'])): ?>
                    <p class="form-error"><?= escape_html($errors['honorific']) ?></p>
                <?php endif; ?>
            </div>

            <!-- Guest Name -->
            <div class="form-group">
                <label for="full_name" class="form-label">Guest Full Name <span class="text-red-500">*</span></label>
                <input 
                    type="text" 
                    id="full_name" 
                    name="full_name" 
                    value="<?= escape_html($old['full_name'] ?? '') ?>" 
                    class="form-input text-xs w-full mt-1.5" 
                    required
                >
                <?php if (isset($errors['full_name'])): ?>
                    <p class="form-error"><?= escape_html($errors['full_name']) ?></p>
                <?php endif; ?>
            </div>

            <!-- Phone Number -->
            <div class="form-group">
                <label for="phone_number" class="form-label">Phone Number (WhatsApp format)</label>
                <input 
                    type="text" 
                    id="phone_number" 
                    name="phone_number" 
                    value="<?= escape_html($old['phone_number'] ?? '') ?>" 
                    placeholder="e.g. +6281234567890" 
                    class="form-input text-xs w-full mt-1.5"
                >
                <span class="text-[10px] text-slate-400 block mt-1 leading-relaxed">Required if you intend to send invitation links via WhatsApp.</span>
                <?php if (isset($errors['phone_number'])): ?>
                    <p class="form-error"><?= escape_html($errors['phone_number']) ?></p>
                <?php endif; ?>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <!-- Pax -->
                <div class="form-group">
                    <label for="invited_pax" class="form-label">Invited Pax</label>
                    <input 
                        type="number" 
                        id="invited_pax" 
                        name="invited_pax" 
                        min="1" 
                        max="255" 
                        value="<?= escape_html($old['invited_pax'] ?? '1') ?>" 
                        class="form-input text-xs w-full mt-1.5"
                    >
                    <?php if (isset($errors['invited_pax'])): ?>
                        <p class="form-error"><?= escape_html($errors['invited_pax']) ?></p>
                    <?php endif; ?>
                </div>

                <!-- Category/Group -->
                <div class="form-group">
                    <label for="guest_group" class="form-label">Category</label>
                    <select id="guest_group" name="guest_group" class="form-select text-xs w-full mt-1.5">
                        <option value="friends" <?= (($old['guest_group'] ?? 'friends') === 'friends') ? 'selected' : '' ?>>Friends</option>
                        <option value="family" <?= (($old['guest_group'] ?? '') === 'family') ? 'selected' : '' ?>>Family</option>
                        <option value="relatives" <?= (($old['guest_group'] ?? '') === 'relatives') ? 'selected' : '' ?>>Relatives</option>
                        <option value="colleagues" <?= (($old['guest_group'] ?? '') === 'colleagues') ? 'selected' : '' ?>>Colleagues</option>
                        <option value="vip" <?= (($old['guest_group'] ?? '') === 'vip') ? 'selected' : '' ?>>VIP</option>
                        <option value="church" <?= (($old['guest_group'] ?? '') === 'church') ? 'selected' : '' ?>>Church</option>
                        <option value="business_partners" <?= (($old['guest_group'] ?? '') === 'business_partners') ? 'selected' : '' ?>>Business Partners</option>
                        <option value="others" <?= (($old['guest_group'] ?? '') === 'others') ? 'selected' : '' ?>>Others</option>
                    </select>
                    <?php if (isset($errors['guest_group'])): ?>
                        <p class="form-error"><?= escape_html($errors['guest_group']) ?></p>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Side -->
            <div class="form-group">
                <label for="side" class="form-label">Guest Invitation Side</label>
                <select id="side" name="side" class="form-select text-xs w-full mt-1.5">
                    <option value="couple" <?= (($old['side'] ?? 'couple') === 'couple') ? 'selected' : '' ?>>Both Groom &amp; Bride</option>
                    <option value="groom" <?= (($old['side'] ?? '') === 'groom') ? 'selected' : '' ?>>Groom's Side</option>
                    <option value="bride" <?= (($old['side'] ?? '') === 'bride') ? 'selected' : '' ?>>Bride's Side</option>
                </select>
            </div>

            <!-- Notes -->
            <div class="form-group">
                <label for="notes" class="form-label">Notes (Internal metadata)</label>
                <textarea 
                    id="notes" 
                    name="notes" 
                    rows="3" 
                    placeholder="e.g. Table assignment, VIP seating notes..." 
                    class="form-textarea text-xs w-full mt-1.5"
                ><?= escape_html($old['notes'] ?? '') ?></textarea>
            </div>

            <!-- Event Access & Session Assignment -->
            <div class="form-group">
                <label class="form-label block mb-2 font-bold">Event Access &amp; Session Assignment</label>
                <div class="flex flex-col gap-3 mt-2 bg-slate-50 border border-slate-200 rounded-xl p-4">
                    <span class="text-[10px] text-slate-400 block mb-1 leading-relaxed">Specify which wedding ceremonies this guest is invited to and select their assigned invitation session:</span>
                    <?php foreach ($workspace_events as $we): 
                        $eventId = (int) $we['id'];
                        $isChecked = true; 
                        $sessions = $event_sessions[$eventId] ?? [];
                        $hasMultipleSessions = count($sessions) > 1;
                    ?>
                        <div class="bg-white border border-slate-200 rounded-lg p-3 flex flex-col sm:flex-row sm:items-center justify-between gap-3 shadow-2xs">
                            <label class="flex items-center gap-3 text-xs text-slate-800 font-semibold cursor-pointer hover:text-slate-900 transition">
                                <input 
                                    type="checkbox" 
                                    name="event_ids[]" 
                                    value="<?= $eventId ?>" 
                                    <?= $isChecked ? 'checked' : '' ?>
                                    class="rounded border-slate-300 text-blue-600 focus:ring-blue-500"
                                >
                                <span><?= escape_html($we['event_name']) ?></span>
                            </label>

                            <?php if (!empty($sessions)): ?>
                                <?php if ($hasMultipleSessions): ?>
                                    <!-- Multiple Sessions: Dropdown Select -->
                                    <div class="flex items-center gap-2 pl-7 sm:pl-0">
                                        <label for="session_<?= $eventId ?>" class="text-[11px] text-slate-500 font-medium whitespace-nowrap">Session:</label>
                                        <select 
                                            id="session_<?= $eventId ?>" 
                                            name="event_sessions[<?= $eventId ?>]" 
                                            class="form-select text-xs py-1 px-2.5 rounded-md border-slate-300 bg-white focus:ring-blue-500 focus:border-blue-500 min-w-[180px]"
                                        >
                                            <?php foreach ($sessions as $s): 
                                                $sId = (int) $s['id'];
                                                $isDefault = (int) ($s['is_default'] ?? 0) === 1;
                                                $timeFormatted = (new \DateTime($s['start_time']))->format('H:i');
                                            ?>
                                                <option value="<?= $sId ?>" <?= $isDefault ? 'selected' : '' ?>>
                                                    <?= escape_html($s['session_name']) ?> (<?= $timeFormatted ?>)
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                <?php else: 
                                    $singleSession = $sessions[0];
                                    $timeFormatted = (new \DateTime($singleSession['start_time']))->format('H:i');
                                ?>
                                    <!-- Single Session: Read-only Badge, Auto-assigned -->
                                    <div class="pl-7 sm:pl-0 flex items-center gap-2">
                                        <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-[11px] font-medium bg-slate-100 text-slate-600 border border-slate-200">
                                            <i class="fa-regular fa-clock text-[10px] text-slate-400"></i>
                                            <?= escape_html($singleSession['session_name']) ?> (<?= $timeFormatted ?>)
                                        </span>
                                        <input type="hidden" name="event_sessions[<?= $eventId ?>]" value="<?= (int) $singleSession['id'] ?>">
                                    </div>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <!-- Actions -->
            <div class="pt-4 border-t border-slate-100 flex justify-end gap-3">
                <a href="<?= url("/app/{$role}/guests") ?>" class="btn btn-secondary text-xs font-semibold px-5 py-2.5">
                    Cancel
                </a>
                <button type="submit" class="btn btn-primary text-xs font-semibold px-5 py-2.5 flex items-center gap-1.5">
                    <i class="fa-solid fa-floppy-disk"></i> Save Guest
                </button>
            </div>
        </form>
    </div>

</div>
