<?php
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) : '';
$page_kicker = sanitize_text_field((string) $this->get_event_hero_kicker_text());

echo $this->render_notice();
echo $this->render_platform_breadcrumbs($page_title); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>

<section class="eep-page-banner">
    <p class="eep-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="eep-account-shell">
    <section class="eep-my-events eep-whos-attending-wrap">
        <?php if ($event_id > 0 && $event_title !== '') : ?>
            <header class="eep-whos-attending-head">
                <h3><?php echo esc_html($event_title); ?></h3>
                <p class="eep-meta">
                    <?php
                    echo esc_html(sprintf(
                        __('RSVP: %1$d | Waitlist: %2$d', 'nexdirectory'),
                        $confirmed_count,
                        $waitlist_count
                    ));
                    ?>
                </p>
                <p class="eep-whos-attending-actions">
                    <?php if ($event_url !== '') : ?>
                        <a class="eep-btn eep-btn-secondary eep-btn-sm" href="<?php echo esc_url($event_url); ?>"><?php esc_html_e('Open Listing', 'nexdirectory'); ?></a>
                    <?php endif; ?>
                <a class="eep-btn eep-btn-sm eep-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="eep-table-wrap">
                <table class="eep-admin-table eep-responsive-table eep-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="eep-status-pill <?php echo (string) ($row['status'] ?? '') === 'waitlist' ? 'eep-status-pending' : 'eep-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="eep-empty-state eep-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="eep-btn eep-force-accent-action" href="<?php echo esc_url($events_url); ?>"><?php esc_html_e('Browse Listings', 'nexdirectory'); ?></a>
            </div>
        <?php endif; ?>
    </section>
</div>
