<?php
if (!defined('ABSPATH')) {
    exit;
}

$brand_name = sanitize_text_field((string) ($brand_name ?? __('nexdirectory', 'nexdirectory')));
if ($brand_name === '') {
    $brand_name = __('nexdirectory', 'nexdirectory');
}
$brand_logo_url = esc_url((string) ($brand_logo_url ?? ''));

$accent_color = sanitize_hex_color((string) ($accent_color ?? '#FF5E14')) ?: '#FF5E14';
$accent_hover_color = sanitize_hex_color((string) ($accent_hover_color ?? '#E04F0D')) ?: '#E04F0D';
$on_primary_color = sanitize_hex_color((string) ($on_primary_color ?? '#FFFFFF')) ?: '#FFFFFF';

$verification_state = sanitize_key((string) ($verification_state ?? 'error'));
if (!in_array($verification_state, ['success', 'warning', 'error'], true)) {
    $verification_state = 'error';
}

$state_map = [
    'success' => [
        'dot' => 'bg-emerald-500',
        'badge' => 'bg-emerald-100 text-emerald-800 border border-emerald-200',
        'icon_wrap' => 'bg-emerald-500',
        'icon' => 'M5 13l4 4L19 7',
    ],
    'warning' => [
        'dot' => 'bg-amber-500',
        'badge' => 'bg-amber-100 text-amber-800 border border-amber-200',
        'icon_wrap' => 'bg-amber-500',
        'icon' => 'M12 9v3m0 4h.01M10.29 3.86l-8.32 14A1 1 0 002.82 19h18.36a1 1 0 00.86-1.5l-8.32-14a1 1 0 00-1.72 0z',
    ],
    'error' => [
        'dot' => 'bg-rose-500',
        'badge' => 'bg-rose-100 text-rose-800 border border-rose-200',
        'icon_wrap' => 'bg-rose-500',
        'icon' => 'M6 18L18 6M6 6l12 12',
    ],
];
$state_ui = $state_map[$verification_state];

$verification_state_label = sanitize_text_field((string) ($verification_state_label ?? __('Not verified', 'nexdirectory')));
$verification_title = sanitize_text_field((string) ($verification_title ?? __('Ticket verification result', 'nexdirectory')));
$verification_message = sanitize_text_field((string) ($verification_message ?? __('Verification details are shown below.', 'nexdirectory')));

$ticket_uid = sanitize_text_field((string) ($ticket_uid ?? ''));
$status_label = sanitize_text_field((string) ($status_label ?? __('Unknown', 'nexdirectory')));
$amount_label = sanitize_text_field((string) ($amount_label ?? __('N/A', 'nexdirectory')));
$purchased_label = sanitize_text_field((string) ($purchased_label ?? __('N/A', 'nexdirectory')));
$scanned_at_label = sanitize_text_field((string) ($scanned_at_label ?? __('N/A', 'nexdirectory')));
$event_title = sanitize_text_field((string) ($event_title ?? __('Event', 'nexdirectory')));
$event_url = esc_url((string) ($event_url ?? ''));
$event_date_label = sanitize_text_field((string) ($event_date_label ?? __('N/A', 'nexdirectory')));
$event_location_label = sanitize_text_field((string) ($event_location_label ?? __('N/A', 'nexdirectory')));
$attendee_label = sanitize_text_field((string) ($attendee_label ?? __('Guest', 'nexdirectory')));
$payment_method_label = sanitize_text_field((string) ($payment_method_label ?? __('N/A', 'nexdirectory')));
$gateway_label = sanitize_text_field((string) ($gateway_label ?? __('N/A', 'nexdirectory')));
$rsvp_status_label = sanitize_text_field((string) ($rsvp_status_label ?? __('N/A', 'nexdirectory')));
$external_order_id = sanitize_text_field((string) ($external_order_id ?? ''));
$external_payment_id = sanitize_text_field((string) ($external_payment_id ?? ''));
$ticket_order_id = absint((int) ($ticket_order_id ?? 0));
$fallback_url = esc_url((string) ($fallback_url ?? home_url('/')));
$support_email = sanitize_email((string) ($support_email ?? ''));
$qr_image_url = esc_url((string) ($qr_image_url ?? ''));
$qr_payload_pretty = trim((string) ($qr_payload_pretty ?? ''));
$show_sensitive_details = !empty($show_sensitive_details);

$page_title = $ticket_uid !== ''
    ? sprintf(__('Ticket Verification • %s', 'nexdirectory'), $ticket_uid)
    : __('Ticket Verification', 'nexdirectory');
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="noindex,nofollow,noarchive">
    <title><?php echo esc_html($page_title); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        brand: {
                            500: <?php echo wp_json_encode($accent_color); ?>,
                            600: <?php echo wp_json_encode($accent_hover_color); ?>
                        }
                    }
                }
            }
        };
    </script>
    <style>
        :root { color-scheme: light; --eep-on-primary: <?php echo esc_html($on_primary_color); ?>; }
        html, body { margin: 0; min-height: 100%; background: #f8fafc; }
        .eep-gradient-orb {
            background: radial-gradient(closest-side, color-mix(in srgb, <?php echo esc_html($accent_color); ?> 45%, #ffffff) 0%, rgba(255,255,255,0) 100%);
        }
    </style>
</head>
<body class="min-h-screen bg-slate-50 text-slate-900 antialiased">
    <div class="fixed inset-0 -z-10 overflow-hidden pointer-events-none">
        <div class="eep-gradient-orb absolute -top-24 -left-12 h-80 w-80 rounded-full opacity-90"></div>
        <div class="eep-gradient-orb absolute bottom-0 right-0 h-96 w-96 rounded-full opacity-70"></div>
    </div>

    <main class="mx-auto w-full max-w-5xl px-4 py-8 sm:px-6 lg:px-8" x-data="{ showPayload: false }">
        <header class="mb-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm sm:p-6">
            <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
                <div class="flex items-center gap-3">
                    <?php if ($brand_logo_url !== '') : ?>
                        <img src="<?php echo esc_url($brand_logo_url); ?>" alt="<?php echo esc_attr($brand_name); ?>" class="h-10 w-auto max-w-[180px] object-contain">
                    <?php endif; ?>
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-[0.15em] text-slate-500"><?php esc_html_e('Ticket Scanner', 'nexdirectory'); ?></p>
                        <h1 class="text-xl font-extrabold tracking-tight text-slate-900 sm:text-2xl"><?php echo esc_html($brand_name); ?></h1>
                    </div>
                </div>
                <span class="inline-flex items-center gap-2 rounded-full px-3 py-1 text-xs font-semibold <?php echo esc_attr($state_ui['badge']); ?>">
                    <span class="h-2.5 w-2.5 rounded-full <?php echo esc_attr($state_ui['dot']); ?>"></span>
                    <?php echo esc_html($verification_state_label); ?>
                </span>
            </div>
        </header>

        <section class="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm sm:p-6">
            <div class="mb-6 flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
                <div class="flex items-start gap-4">
                    <div class="mt-0.5 flex h-12 w-12 shrink-0 items-center justify-center rounded-xl text-white <?php echo esc_attr($state_ui['icon_wrap']); ?>">
                        <?php if ($verification_state === 'warning') : ?>
                            <svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="<?php echo esc_attr($state_ui['icon']); ?>"></path>
                            </svg>
                        <?php else : ?>
                            <svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.3" d="<?php echo esc_attr($state_ui['icon']); ?>"></path>
                            </svg>
                        <?php endif; ?>
                    </div>
                    <div>
                        <h2 class="text-2xl font-black tracking-tight text-slate-900"><?php echo esc_html($verification_title); ?></h2>
                        <p class="mt-1 text-sm text-slate-600 sm:text-base"><?php echo esc_html($verification_message); ?></p>
                    </div>
                </div>
                <p class="text-xs font-medium uppercase tracking-[0.12em] text-slate-500">
                    <?php esc_html_e('Scanned', 'nexdirectory'); ?>: <span class="normal-case tracking-normal text-slate-700"><?php echo esc_html($scanned_at_label); ?></span>
                </p>
            </div>

            <div class="grid gap-4 sm:grid-cols-2">
                <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                    <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Ticket ID', 'nexdirectory'); ?></p>
                    <p class="mt-1 break-all text-base font-bold text-slate-900"><?php echo esc_html($ticket_uid !== '' ? $ticket_uid : __('N/A', 'nexdirectory')); ?></p>
                </article>
                <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                    <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Status', 'nexdirectory'); ?></p>
                    <p class="mt-1 text-base font-bold text-slate-900"><?php echo esc_html($status_label); ?></p>
                </article>
                <?php if ($show_sensitive_details) : ?>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Amount', 'nexdirectory'); ?></p>
                        <p class="mt-1 text-base font-bold text-slate-900"><?php echo esc_html($amount_label); ?></p>
                    </article>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Purchased', 'nexdirectory'); ?></p>
                        <p class="mt-1 text-base font-bold text-slate-900"><?php echo esc_html($purchased_label); ?></p>
                    </article>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Attendee', 'nexdirectory'); ?></p>
                        <p class="mt-1 text-base font-bold text-slate-900"><?php echo esc_html($attendee_label); ?></p>
                    </article>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('RSVP', 'nexdirectory'); ?></p>
                        <p class="mt-1 text-base font-bold text-slate-900"><?php echo esc_html($rsvp_status_label); ?></p>
                    </article>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Payment Method', 'nexdirectory'); ?></p>
                        <p class="mt-1 text-base font-bold text-slate-900"><?php echo esc_html($payment_method_label); ?></p>
                    </article>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Gateway', 'nexdirectory'); ?></p>
                        <p class="mt-1 text-base font-bold text-slate-900"><?php echo esc_html($gateway_label); ?></p>
                    </article>
                <?php else : ?>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4 sm:col-span-2">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Restricted', 'nexdirectory'); ?></p>
                        <p class="mt-1 text-sm font-semibold text-slate-700"><?php esc_html_e('Sign in as the ticket holder, event owner, or moderator to view attendee and payment details.', 'nexdirectory'); ?></p>
                    </article>
                <?php endif; ?>
            </div>

            <div class="mt-5 rounded-xl border border-slate-200 bg-white p-4">
                <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Event', 'nexdirectory'); ?></p>
                <p class="mt-1 text-lg font-black tracking-tight text-slate-900"><?php echo esc_html($event_title); ?></p>
                <div class="mt-2 grid gap-1 text-sm text-slate-600 sm:grid-cols-2">
                    <p><span class="font-semibold text-slate-700"><?php esc_html_e('Date', 'nexdirectory'); ?>:</span> <?php echo esc_html($event_date_label); ?></p>
                    <p><span class="font-semibold text-slate-700"><?php esc_html_e('Location', 'nexdirectory'); ?>:</span> <?php echo esc_html($event_location_label); ?></p>
                </div>
            </div>

            <?php if ($show_sensitive_details && ($external_order_id !== '' || $external_payment_id !== '' || $ticket_order_id > 0)) : ?>
                <div class="mt-5 grid gap-4 sm:grid-cols-3">
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('Order Row ID', 'nexdirectory'); ?></p>
                        <p class="mt-1 break-all text-sm font-bold text-slate-900"><?php echo esc_html($ticket_order_id > 0 ? (string) $ticket_order_id : __('N/A', 'nexdirectory')); ?></p>
                    </article>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('External Order', 'nexdirectory'); ?></p>
                        <p class="mt-1 break-all text-sm font-bold text-slate-900"><?php echo esc_html($external_order_id !== '' ? $external_order_id : __('N/A', 'nexdirectory')); ?></p>
                    </article>
                    <article class="rounded-xl border border-slate-200 bg-slate-50 p-4">
                        <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('External Payment', 'nexdirectory'); ?></p>
                        <p class="mt-1 break-all text-sm font-bold text-slate-900"><?php echo esc_html($external_payment_id !== '' ? $external_payment_id : __('N/A', 'nexdirectory')); ?></p>
                    </article>
                </div>
            <?php endif; ?>

            <?php if ($show_sensitive_details && $qr_image_url !== '') : ?>
                <div class="mt-5 rounded-xl border border-slate-200 bg-slate-50 p-4">
                    <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-slate-500"><?php esc_html_e('QR Record', 'nexdirectory'); ?></p>
                    <img src="<?php echo esc_url($qr_image_url); ?>" alt="<?php esc_attr_e('Ticket QR code', 'nexdirectory'); ?>" class="mt-2 h-28 w-28 rounded-lg border border-slate-200 bg-white object-cover">
                </div>
            <?php endif; ?>

            <?php if ($show_sensitive_details && $qr_payload_pretty !== '') : ?>
                <div class="mt-5 rounded-xl border border-slate-200 bg-white p-4">
                    <button type="button" @click="showPayload = !showPayload" class="inline-flex items-center gap-2 text-sm font-semibold text-brand-600 hover:text-brand-600">
                        <span><?php esc_html_e('Show raw QR payload', 'nexdirectory'); ?></span>
                        <svg class="h-4 w-4 transition-transform" :class="showPayload ? 'rotate-180' : ''" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
                        </svg>
                    </button>
                    <pre x-show="showPayload" x-transition class="mt-3 overflow-auto rounded-lg bg-slate-900 p-3 text-xs text-slate-100"><?php echo esc_html($qr_payload_pretty); ?></pre>
                </div>
            <?php endif; ?>

            <div class="mt-6 flex flex-col gap-3 sm:flex-row">
                <?php if ($event_url !== '') : ?>
                    <a href="<?php echo esc_url($event_url); ?>" class="inline-flex items-center justify-center rounded-xl bg-brand-500 px-5 py-3 text-sm font-bold hover:bg-brand-600" style="color: <?php echo esc_attr($on_primary_color); ?>;">
                        <?php esc_html_e('Open Event', 'nexdirectory'); ?>
                    </a>
                <?php endif; ?>
                <?php if ($fallback_url !== '') : ?>
                    <a href="<?php echo esc_url($fallback_url); ?>" class="inline-flex items-center justify-center rounded-xl border border-slate-300 bg-white px-5 py-3 text-sm font-bold text-slate-700 hover:bg-slate-100">
                        <?php esc_html_e('Back', 'nexdirectory'); ?>
                    </a>
                <?php endif; ?>
                <?php if ($support_email !== '') : ?>
                    <a href="mailto:<?php echo esc_attr($support_email); ?>" class="inline-flex items-center justify-center rounded-xl border border-slate-300 bg-white px-5 py-3 text-sm font-bold text-slate-700 hover:bg-slate-100">
                        <?php esc_html_e('Contact Support', 'nexdirectory'); ?>
                    </a>
                <?php endif; ?>
            </div>
        </section>
    </main>
</body>
</html>
