<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Templates use local variables supplied by the render context.
if (!defined('ABSPATH')) {
    exit;
}

$ui_labels = isset($ui_labels) && is_array($ui_labels) ? $ui_labels : [];
$profile_key = isset($profile_key) ? sanitize_key((string) $profile_key) : sanitize_key((string) ($ui_labels['profile_key'] ?? ''));
$is_events_profile = isset($is_events_profile) ? (bool) $is_events_profile : ((string) ($ui_labels['is_events_profile'] ?? '1') === '1');
$page_title = isset($page_title) ? sanitize_text_field((string) $page_title) : __('Who\'s Attending', 'nexdirectory');
$page_description = isset($page_description) ? sanitize_text_field((string) $page_description) : __('See attendee details for this listing.', 'nexdirectory');
$events_url = isset($events_url) ? esc_url((string) $events_url) : esc_url($this->get_profile_route_default_url('events', $profile_key));
$event_id = isset($event_id) ? absint((int) $event_id) : 0;
$event_title = isset($event_title) ? sanitize_text_field((string) $event_title) : '';
$event_url = isset($event_url) ? esc_url((string) $event_url) : '';
$rows = isset($rows) && is_array($rows) ? array_values($rows) : [];
$status_filter = isset($status_filter) ? sanitize_key((string) $status_filter) : 'yes';
$confirmed_count = isset($confirmed_count) ? absint((int) $confirmed_count) : 0;
$waitlist_count = isset($waitlist_count) ? absint((int) $waitlist_count) : 0;
$state_message = isset($state_message) ? sanitize_text_field((string) $state_message) : '';
$privacy_notice = isset($privacy_notice) ? sanitize_text_field((string) $privacy_notice) : '';
$can_view_private = !empty($can_view_private);
$page_kicker = sanitize_text_field((string) $this->get_event_hero_kicker_text());

echo wp_kses_post($this->render_notice());
$nexdirectory_breadcrumb_markup = NEXDIRECTORY_Escaping::prepare_markup_for_wp_kses((string) $this->render_platform_breadcrumbs($page_title));
echo wp_kses($nexdirectory_breadcrumb_markup, NEXDIRECTORY_Escaping::allowed_html($nexdirectory_breadcrumb_markup));
?>

<section class="nexdirectory-page-banner">
    <p class="nexdirectory-page-kicker"><?php echo esc_html($page_kicker); ?></p>
    <h1><?php echo esc_html($page_title); ?></h1>
    <p><?php echo esc_html($page_description); ?></p>
</section>

<div class="nexdirectory-account-shell">
    <section class="nexdirectory-my-events nexdirectory-whos-attending-wrap">
        <?php if ($event_id > 0 && $event_title !== '') : ?>
            <header class="nexdirectory-whos-attending-head">
                <h3><?php echo esc_html($event_title); ?></h3>
                <p class="nexdirectory-meta">
                    <?php
                    if ($can_view_private) {
                        echo esc_html(sprintf(
                            /* translators: 1: Confirmed attendee count. 2: Private waitlist count visible only to the listing owner or moderator. */
                            __('Confirmed: %1$d · Waitlist: %2$d', 'nexdirectory'),
                            $confirmed_count,
                            $waitlist_count
                        ));
                    } else {
                        echo esc_html(sprintf(
                            /* translators: %d: Confirmed attendee count. */
                            _n('%d confirmed attendee', '%d confirmed attendees', $confirmed_count, 'nexdirectory'),
                            $confirmed_count
                        ));
                    }
                    ?>
                </p>
                <?php if ($privacy_notice !== '') : ?>
                    <p class="mt-2 inline-flex rounded-lg border border-blue-100 bg-blue-50 px-3 py-2 text-sm text-blue-700"><?php echo esc_html($privacy_notice); ?></p>
                <?php endif; ?>
                <p class="nexdirectory-whos-attending-actions">
                    <?php if ($event_url !== '') : ?>
                        <a class="nexdirectory-btn nexdirectory-btn-secondary nexdirectory-btn-sm" href="<?php echo esc_url($event_url); ?>"><?php esc_html_e('Open Listing', 'nexdirectory'); ?></a>
                    <?php endif; ?>
                <a class="nexdirectory-btn nexdirectory-btn-sm nexdirectory-force-accent-action" href="<?php echo esc_url($events_url); ?>"><?php esc_html_e('Browse Listings', 'nexdirectory'); ?></a>
                </p>
            </header>
        <?php endif; ?>

        <?php if (!empty($rows)) : ?>
            <div class="nexdirectory-table-wrap">
                <table class="nexdirectory-admin-table nexdirectory-responsive-table nexdirectory-whos-attending-table">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Name', 'nexdirectory'); ?></th>
                            <?php if ($status_filter === 'all') : ?>
                                <th><?php esc_html_e('Status', 'nexdirectory'); ?></th>
                            <?php endif; ?>
                            <th><?php esc_html_e('Updated', 'nexdirectory'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($rows as $row) : ?>
                            <tr>
                                <td data-label="<?php esc_attr_e('Name', 'nexdirectory'); ?>"><?php echo esc_html((string) ($row['name'] ?? '')); ?></td>
                                <?php if ($status_filter === 'all') : ?>
                                    <td data-label="<?php esc_attr_e('Status', 'nexdirectory'); ?>">
                                        <span class="nexdirectory-status-pill <?php echo (string) ($row['status'] ?? '') === 'waitlist' ? 'nexdirectory-status-pending' : 'nexdirectory-status-publish'; ?>">
                                            <?php echo esc_html((string) ($row['status_label'] ?? '')); ?>
                                        </span>
                                    </td>
                                <?php endif; ?>
                                <td data-label="<?php esc_attr_e('Updated', 'nexdirectory'); ?>"><?php echo esc_html((string) ($row['updated_label'] ?? '')); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else : ?>
            <div class="nexdirectory-empty-state nexdirectory-whos-attending-empty-state">
                <h3><?php echo esc_html($is_events_profile ? __('No RSVPs Yet', 'nexdirectory') : __('No Followers Yet', 'nexdirectory')); ?></h3>
                <p><?php echo esc_html($state_message !== '' ? $state_message : __('No records found for this listing.', 'nexdirectory')); ?></p>
                <a class="nexdirectory-btn nexdirectory-force-accent-action" href="<?php echo esc_url($events_url); ?>"><?php esc_html_e('Browse Listings', 'nexdirectory'); ?></a>
            </div>
        <?php endif; ?>
    </section>
</div>
