<?php
if (!defined('ABSPATH')) {
    exit;
}
$ui_labels = isset($ui_labels) && is_array($ui_labels) ? $ui_labels : [];
$profile_key = sanitize_key((string) ($profile_key ?? ($ui_labels['profile_key'] ?? '')));
$boost_title = sanitize_text_field((string) ($boost_title ?? __('Boost Post', 'nexdirectory')));
$boost_description = sanitize_text_field((string) ($boost_description ?? __('Promote your listings with paid placement and renewal controls.', 'nexdirectory')));
$boosts_enabled = !empty($boosts_enabled);
$boost_test_free_mode = $boosts_enabled && method_exists($this, 'boost_test_free_mode_enabled') ? (bool) $this->boost_test_free_mode_enabled() : false;
$boost_auto_renew_module = $boosts_enabled && !$boost_test_free_mode && method_exists($this, 'boost_auto_renew_enabled') ? (bool) $this->boost_auto_renew_enabled() : false;
$boost_duration_pricing = $boosts_enabled && method_exists($this, 'get_boost_duration_pricing') ? (array) $this->get_boost_duration_pricing() : [];
$boost_gateway_stripe_enabled = $boosts_enabled && (string) get_option('eep_boost_stripe_enabled', '0') === '1';
$boost_gateway_paypal_enabled = $boosts_enabled && (string) get_option('eep_boost_paypal_enabled', '0') === '1';
$boost_checkout_available = $boosts_enabled && ($boost_test_free_mode || $boost_gateway_stripe_enabled || $boost_gateway_paypal_enabled);
$boost_currency = sanitize_text_field((string) get_option('eep_boost_currency', 'USD'));
$boost_badge_label = sanitize_text_field((string) get_option('eep_boost_featured_badge_text', __('Featured', 'nexdirectory')));
$boost_history_rows = isset($boost_history_rows) && is_array($boost_history_rows) ? $boost_history_rows : [];
$submit_event_url = isset($submit_event_url) ? (string) $submit_event_url : $this->get_platform_page_url(
    'eep_page_submit_event_url',
    $this->get_profile_route_slugs('submit', $profile_key),
    $this->get_profile_route_default_url('submit', $profile_key)
);
$my_events_url = isset($my_events_url) ? (string) $my_events_url : $this->get_platform_page_url(
    'eep_page_my_events_url',
    $this->get_profile_route_slugs('my_items', $profile_key),
    $this->get_profile_route_default_url('my_items', $profile_key)
);
$plural_label = sanitize_text_field((string) ($ui_labels['plural'] ?? __('Listings', 'nexdirectory')));
$edit_action_label = sanitize_text_field((string) ($ui_labels['single_edit_cta'] ?? __('Edit Event', 'nexdirectory')));
$page_kicker = sanitize_text_field((string) $this->get_event_hero_kicker_text());
$status_class_map = [
    'active' => 'publish',
    'paid' => 'publish',
    'renewed' => 'publish',
    'pending' => 'pending',
    'renewal_failed' => 'pending',
    'cancelled' => 'draft',
    'failed' => 'rejected',
    'expired' => 'draft',
];
$boost_listing_count = 0;
$boost_active_count = 0;
$boost_expiring_soon_count = 0;
$next_expiry_ts = 0;
$now_ts = time();
if ($query instanceof WP_Query && !empty($query->posts) && is_array($query->posts)) {
    foreach ($query->posts as $boost_post_obj) {
        if (!$boost_post_obj instanceof WP_Post) {
            continue;
        }
        $boost_listing_count++;
        $state = method_exists($this, 'get_listing_boost_state') ? (array) $this->get_listing_boost_state((int) $boost_post_obj->ID) : [];
        if (empty($state['active'])) {
            continue;
        }
        $boost_active_count++;
        $expires_ts = isset($state['expires_ts']) ? absint((int) $state['expires_ts']) : 0;
        if ($expires_ts > $now_ts && $expires_ts <= ($now_ts + (7 * DAY_IN_SECONDS))) {
            $boost_expiring_soon_count++;
        }
        if ($expires_ts > 0 && ($next_expiry_ts <= 0 || $expires_ts < $next_expiry_ts)) {
            $next_expiry_ts = $expires_ts;
        }
    }
}
$next_expiry_label = $next_expiry_ts > 0 ? wp_date(get_option('date_format') . ' ' . get_option('time_format'), $next_expiry_ts) : '';
$boost_benefits = [
    __('Higher ranking in discovery feeds and archive pages.', 'nexdirectory'),
    __('Featured badge visibility to improve listing trust and CTR.', 'nexdirectory'),
    __('Priority-first ordering by boost score, then by freshness.', 'nexdirectory'),
];
$boost_steps = [
    __('Select the listing you want to push up in search/discovery.', 'nexdirectory'),
    __('Choose a package duration that matches your campaign window.', 'nexdirectory'),
    $boost_test_free_mode
        ? __('Free test mode is active, so boost activates instantly with no checkout.', 'nexdirectory')
        : __('Complete payment and confirm checkout to activate priority ranking.', 'nexdirectory'),
    __('Track countdown, renew, and monitor impact in analytics.', 'nexdirectory'),
];
echo $this->render_notice();
echo $this->render_platform_breadcrumbs($boost_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($boost_title); ?></h1>
    <p><?php echo esc_html($boost_description); ?></p>
</section>
<?php echo $this->render_account_tabs('boost-posts', $profile_key); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div class="eep-account-shell">
    <div class="eep-my-events eep-boost-posts-shell">
        <section class="eep-boost-sales-hero">
            <div class="eep-boost-sales-copy">
                <h3><?php esc_html_e('Boost visibility and accelerate enquiries', 'nexdirectory'); ?></h3>
                <p><?php esc_html_e('Boost Post gives your listing stronger placement and faster discovery. It is designed for launches, time-sensitive updates, and premium campaigns.', 'nexdirectory'); ?></p>
                <ul class="eep-boost-benefits-list">
                    <?php foreach ($boost_benefits as $benefit) : ?>
                        <li><?php echo esc_html((string) $benefit); ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <div class="eep-boost-sales-stats">
                <article>
                    <span><?php esc_html_e('Total Listings', 'nexdirectory'); ?></span>
                    <strong><?php echo esc_html((string) $boost_listing_count); ?></strong>
                </article>
                <article>
                    <span><?php esc_html_e('Active Boosts', 'nexdirectory'); ?></span>
                    <strong><?php echo esc_html((string) $boost_active_count); ?></strong>
                </article>
                <article>
                    <span><?php esc_html_e('Expiring in 7 Days', 'nexdirectory'); ?></span>
                    <strong><?php echo esc_html((string) $boost_expiring_soon_count); ?></strong>
                </article>
                <article>
                    <span><?php esc_html_e('Next Expiry', 'nexdirectory'); ?></span>
                    <strong><?php echo esc_html($next_expiry_label !== '' ? $next_expiry_label : __('N/A', 'nexdirectory')); ?></strong>
                </article>
            </div>
        </section>

        <?php if (!empty($boost_duration_pricing)) : ?>
            <section class="eep-boost-package-grid">
                <?php foreach ($boost_duration_pricing as $package_days => $package_price) : ?>
                    <?php
                    $package_days = absint((int) $package_days);
                    $package_price = is_numeric($package_price) ? (float) $package_price : 0.0;
                    if ($package_days <= 0 || $package_price <= 0) {
                        continue;
                    }
                    $daily_price = $package_days > 0 ? round($package_price / $package_days, 2) : 0.0;
                    ?>
                    <article class="eep-boost-package-card">
                        <h4><?php echo esc_html(sprintf(_n('%d Day Boost', '%d Day Boost', $package_days, 'nexdirectory'), $package_days)); ?></h4>
                        <p class="eep-boost-package-price">
                            <?php
                            echo esc_html($boost_test_free_mode
                                ? __('Free (Test Mode)', 'nexdirectory')
                                : sprintf('%s %s', $boost_currency, number_format($package_price, 2)));
                            ?>
                        </p>
                        <p class="eep-boost-package-meta">
                            <?php
                            echo esc_html($boost_test_free_mode
                                ? __('Use this package to test your boost flow and UI.', 'nexdirectory')
                                : sprintf(__('Approx. %1$s %2$s/day', 'nexdirectory'), $boost_currency, number_format($daily_price, 2)));
                            ?>
                        </p>
                    </article>
                <?php endforeach; ?>
            </section>
        <?php endif; ?>

        <section class="eep-boost-steps">
            <h3><?php esc_html_e('How Boost Post works', 'nexdirectory'); ?></h3>
            <ol>
                <?php foreach ($boost_steps as $step_text) : ?>
                    <li><?php echo esc_html((string) $step_text); ?></li>
                <?php endforeach; ?>
            </ol>
        </section>

        <h3><?php esc_html_e('Boost Controls', 'nexdirectory'); ?></h3>
        <?php if (!$boosts_enabled) : ?>
            <div class="eep-empty-state">
                <h3><?php esc_html_e('Boosting is currently disabled', 'nexdirectory'); ?></h3>
                <p><?php esc_html_e('The site administrator has turned off paid boosts, so this tab is hidden until boosts are enabled again.', 'nexdirectory'); ?></p>
                <a class="eep-btn eep-btn-secondary" href="<?php echo esc_url($my_events_url); ?>"><?php esc_html_e('Go to My Listings', 'nexdirectory'); ?></a>
            </div>
        <?php else : ?>
            <?php if (!$boost_checkout_available) : ?>
                <div class="eep-notice eep-notice-error">
                    <?php esc_html_e('No payment gateways are enabled for boosts. Ask an administrator to enable Stripe, PayPal, or free test mode.', 'nexdirectory'); ?>
                </div>
            <?php endif; ?>
            <?php if ($boost_test_free_mode) : ?>
                <div class="eep-notice eep-notice-success">
                    <?php esc_html_e('Free test mode is enabled. Boosts activate instantly without payment checkout.', 'nexdirectory'); ?>
                </div>
            <?php endif; ?>
            <?php if ($query instanceof WP_Query && $query->have_posts()) : ?>
                <div class="eep-table-wrap">
                    <table class="eep-admin-table eep-responsive-table eep-my-events-table">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Listing', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Priority', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Boost Status', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Time Remaining', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Actions', 'nexdirectory'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php while ($query->have_posts()) : $query->the_post(); ?>
                                <?php
                                $event_status = (string) get_post_status(get_the_ID());
                                $edit_target_id = (int) get_the_ID();
                                $edit_event_url = eep_add_query_arg([
                                    'eep_tab' => 'submit',
                                    'edit_event_id' => $edit_target_id,
                                ], (string) $submit_event_url) . '#submit';
                                $can_edit_target = $this->user_can_edit_event($edit_target_id);
                                $is_demo_locked_target = is_callable([$this, 'is_demo_event_edit_locked']) ? (bool) $this->is_demo_event_edit_locked($edit_target_id, (int) get_current_user_id()) : false;
                                $duplicate_event_url = wp_nonce_url(
                                    eep_add_query_arg([
                                        'action' => 'eep_duplicate_event',
                                        'post' => (int) $edit_target_id,
                                    ], admin_url('admin.php')),
                                    'eep_duplicate_event_' . (int) $edit_target_id
                                );
                                $view_event_url = (string) get_permalink(get_the_ID());
                                $view_event_label = __('View', 'nexdirectory');
                                if ($event_status !== 'publish' && $can_edit_target) {
                                    $preview_url = get_preview_post_link($edit_target_id);
                                    if ((!is_string($preview_url) || $preview_url === '') && $edit_target_id > 0) {
                                        $preview_url = eep_add_query_arg([
                                            'p' => $edit_target_id,
                                            'post_type' => $this::POST_TYPE,
                                            'preview' => 'true',
                                        ], home_url('/'));
                                    }
                                    if (is_string($preview_url) && $preview_url !== '') {
                                        $view_event_url = $preview_url;
                                    } elseif (is_string($edit_event_url) && $edit_event_url !== '') {
                                        $view_event_url = $edit_event_url;
                                    }
                                    $view_event_label = __('Preview', 'nexdirectory');
                                }
                                $boost_state = method_exists($this, 'get_listing_boost_state') ? (array) $this->get_listing_boost_state(get_the_ID()) : [];
                                $boost_active = !empty($boost_state['active']);
                                $boost_expires_label = sanitize_text_field((string) ($boost_state['expires_label'] ?? ''));
                                $boost_expires_ts = isset($boost_state['expires_ts']) ? absint((int) $boost_state['expires_ts']) : 0;
                                $boost_auto_renew = !empty($boost_state['auto_renew']);
                                $priority_score = method_exists($this, 'get_listing_boost_priority_score') ? (float) $this->get_listing_boost_priority_score(get_the_ID()) : ($boost_active ? 10.0 : 0.0);
                                $boost_selected_duration = absint((int) ($boost_state['duration_days'] ?? 0));
                                if ($boost_selected_duration <= 0) {
                                    $boost_selected_duration = method_exists($this, 'get_default_boost_duration_days') ? (int) $this->get_default_boost_duration_days() : 30;
                                }
                                $boost_selected_gateway = sanitize_key((string) ($boost_state['gateway'] ?? 'stripe'));
                                if (!in_array($boost_selected_gateway, ['stripe', 'paypal'], true)) {
                                    $boost_selected_gateway = $boost_gateway_stripe_enabled ? 'stripe' : ($boost_gateway_paypal_enabled ? 'paypal' : 'stripe');
                                }
                                $remaining_seconds = $boost_active && $boost_expires_ts > $now_ts ? ($boost_expires_ts - $now_ts) : 0;
                                $remaining_label = '';
                                if ($remaining_seconds > 0) {
                                    if ($remaining_seconds >= DAY_IN_SECONDS) {
                                        $remaining_days = (int) floor($remaining_seconds / DAY_IN_SECONDS);
                                        $remaining_hours = (int) floor(($remaining_seconds % DAY_IN_SECONDS) / HOUR_IN_SECONDS);
                                        $remaining_label = sprintf(__('%1$dd %2$dh', 'nexdirectory'), $remaining_days, $remaining_hours);
                                    } elseif ($remaining_seconds >= HOUR_IN_SECONDS) {
                                        $remaining_hours = (int) floor($remaining_seconds / HOUR_IN_SECONDS);
                                        $remaining_minutes = (int) floor(($remaining_seconds % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS);
                                        $remaining_label = sprintf(__('%1$dh %2$dm', 'nexdirectory'), $remaining_hours, $remaining_minutes);
                                    } else {
                                        $remaining_minutes = max(1, (int) floor($remaining_seconds / MINUTE_IN_SECONDS));
                                        $remaining_label = sprintf(__('%d minutes', 'nexdirectory'), $remaining_minutes);
                                    }
                                }
                                ?>
                                <tr>
                                    <td class="eep-my-event-title" data-label="<?php esc_attr_e('Listing', 'nexdirectory'); ?>"><?php the_title(); ?></td>
                                    <td data-label="<?php esc_attr_e('Priority', 'nexdirectory'); ?>">
                                        <strong><?php echo esc_html(number_format($priority_score, 2)); ?></strong>
                                    </td>
                                    <td data-label="<?php esc_attr_e('Boost Status', 'nexdirectory'); ?>">
                                        <?php if ($boost_active) : ?>
                                            <span class="eep-status-pill eep-status-publish"><?php echo esc_html($boost_badge_label); ?></span>
                                            <?php if ($boost_expires_label !== '') : ?>
                                                <small><?php echo esc_html(sprintf(__('Expires %s', 'nexdirectory'), $boost_expires_label)); ?></small>
                                            <?php endif; ?>
                                        <?php else : ?>
                                            <span class="eep-status-pill eep-status-draft"><?php esc_html_e('Not boosted', 'nexdirectory'); ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td data-label="<?php esc_attr_e('Time Remaining', 'nexdirectory'); ?>">
                                        <?php if ($boost_active && $remaining_label !== '') : ?>
                                            <span class="eep-boost-countdown-pill"><?php echo esc_html(sprintf(__('Countdown: %s', 'nexdirectory'), $remaining_label)); ?></span>
                                        <?php elseif ($boost_active) : ?>
                                            <small><?php esc_html_e('Ending soon', 'nexdirectory'); ?></small>
                                        <?php else : ?>
                                            <small><?php esc_html_e('N/A', 'nexdirectory'); ?></small>
                                        <?php endif; ?>
                                    </td>
                                    <td class="eep-actions eep-my-events-actions" data-label="<?php esc_attr_e('Actions', 'nexdirectory'); ?>">
                                        <a class="eep-btn eep-btn-secondary eep-btn-sm" href="<?php echo esc_url($view_event_url); ?>"><?php echo esc_html($view_event_label); ?></a>
                                        <?php if ($can_edit_target) : ?>
                                            <a class="eep-btn eep-btn-sm" href="<?php echo esc_url($edit_event_url); ?>"><?php echo esc_html($edit_action_label); ?></a>
                                        <?php elseif ($is_demo_locked_target) : ?>
                                            <?php $demo_locked_edit_tip = __('Demo listing locked. Duplicate to use 1 free event credit, or upgrade.', 'nexdirectory'); ?>
                                            <span class="eep-btn eep-btn-sm" role="button" aria-disabled="true" title="<?php echo esc_attr($demo_locked_edit_tip); ?>" style="opacity:.6;cursor:not-allowed;"><?php echo esc_html($edit_action_label); ?></span>
                                            <a class="eep-btn eep-btn-sm" href="<?php echo esc_url($duplicate_event_url); ?>" title="<?php echo esc_attr($demo_locked_edit_tip); ?>"><?php esc_html_e('Use Free Credit', 'nexdirectory'); ?></a>
                                        <?php endif; ?>
                                        <?php if ($boost_checkout_available) : ?>
                                            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="eep-inline-form eep-my-events-boost-form">
                                                <input type="hidden" name="action" value="eep_boost_checkout">
                                                <input type="hidden" name="post_id" value="<?php echo esc_attr(get_the_ID()); ?>">
                                                <?php wp_nonce_field('eep_boost_checkout_' . get_the_ID(), 'eep_boost_checkout_nonce'); ?>
                                                <div class="eep-my-events-boost-inline">
                                                    <select name="duration_days" aria-label="<?php esc_attr_e('Boost duration', 'nexdirectory'); ?>">
                                                        <?php foreach ((array) $boost_duration_pricing as $boost_days => $boost_price) : ?>
                                                            <?php $boost_days = absint((int) $boost_days); ?>
                                                            <?php $boost_price = is_numeric($boost_price) ? (float) $boost_price : 0.0; ?>
                                                            <?php if ($boost_days <= 0 || $boost_price <= 0) {
                                                                continue;
                                                            } ?>
                                                            <option value="<?php echo esc_attr((string) $boost_days); ?>" <?php selected($boost_selected_duration, $boost_days); ?>>
                                                                <?php
                                                                echo esc_html($boost_test_free_mode
                                                                    ? sprintf(__('%1$d days (Free test mode)', 'nexdirectory'), $boost_days)
                                                                    : sprintf(__('%1$d days (%2$s %3$s)', 'nexdirectory'), $boost_days, $boost_currency, number_format($boost_price, 2)));
                                                                ?>
                                                            </option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                    <?php if ($boost_test_free_mode) : ?>
                                                        <input type="hidden" name="gateway" value="test_free">
                                                        <small><?php esc_html_e('No payment checkout needed in free test mode.', 'nexdirectory'); ?></small>
                                                    <?php else : ?>
                                                        <select name="gateway" aria-label="<?php esc_attr_e('Payment gateway', 'nexdirectory'); ?>">
                                                            <?php if ($boost_gateway_stripe_enabled) : ?>
                                                                <option value="stripe" <?php selected($boost_selected_gateway, 'stripe'); ?>><?php esc_html_e('Stripe', 'nexdirectory'); ?></option>
                                                            <?php endif; ?>
                                                            <?php if ($boost_gateway_paypal_enabled) : ?>
                                                                <option value="paypal" <?php selected($boost_selected_gateway, 'paypal'); ?>><?php esc_html_e('PayPal', 'nexdirectory'); ?></option>
                                                            <?php endif; ?>
                                                        </select>
                                                    <?php endif; ?>
                                                    <?php if ($boost_auto_renew_module) : ?>
                                                        <label class="eep-my-events-boost-renew">
                                                            <input type="checkbox" name="auto_renew" value="1" <?php checked($boost_auto_renew, true); ?>>
                                                            <span><?php esc_html_e('Auto-renew', 'nexdirectory'); ?></span>
                                                        </label>
                                                    <?php endif; ?>
                                                </div>
                                                <button class="eep-btn eep-btn-secondary eep-btn-sm" type="submit"><?php echo esc_html($boost_active ? __('Re-Boost', 'nexdirectory') : __('Boost', 'nexdirectory')); ?></button>
                                            </form>
                                        <?php endif; ?>
                                        <?php if ($boost_active && !$boost_test_free_mode) : ?>
                                            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="eep-inline-form">
                                                <input type="hidden" name="action" value="eep_boost_toggle_autorenew">
                                                <input type="hidden" name="post_id" value="<?php echo esc_attr(get_the_ID()); ?>">
                                                <input type="hidden" name="auto_renew" value="<?php echo esc_attr($boost_auto_renew ? '0' : '1'); ?>">
                                                <?php wp_nonce_field('eep_boost_toggle_' . get_the_ID(), 'eep_boost_toggle_nonce'); ?>
                                                <button class="eep-btn eep-btn-secondary eep-btn-sm" type="submit">
                                                    <?php echo esc_html($boost_auto_renew ? __('Disable Auto-Renew', 'nexdirectory') : __('Enable Auto-Renew', 'nexdirectory')); ?>
                                                </button>
                                            </form>
                                            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="eep-inline-form">
                                                <input type="hidden" name="action" value="eep_boost_cancel_subscription">
                                                <input type="hidden" name="post_id" value="<?php echo esc_attr(get_the_ID()); ?>">
                                                <?php wp_nonce_field('eep_boost_cancel_' . get_the_ID(), 'eep_boost_cancel_nonce'); ?>
                                                <button class="eep-btn eep-btn-secondary eep-btn-sm" type="submit"><?php esc_html_e('Cancel Subscription', 'nexdirectory'); ?></button>
                                            </form>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            <?php else : ?>
                <div class="eep-empty-state">
                    <h3><?php esc_html_e('No listings found', 'nexdirectory'); ?></h3>
                    <p><?php echo esc_html(sprintf(__('Create your first %s to start using paid boosts.', 'nexdirectory'), strtolower((string) $plural_label))); ?></p>
                    <a class="eep-btn eep-force-accent-action" href="<?php echo esc_url($submit_event_url); ?>"><?php esc_html_e('Create Listing', 'nexdirectory'); ?></a>
                </div>
            <?php endif; ?>

            <h3><?php esc_html_e('Boost History', 'nexdirectory'); ?></h3>
            <?php if (!empty($boost_history_rows)) : ?>
                <div class="eep-table-wrap">
                    <table class="eep-admin-table eep-responsive-table">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Listing', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Package', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Amount', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Gateway', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Status', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Ordered', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Expires', 'nexdirectory'); ?></th>
                                <th><?php esc_html_e('Auto-Renew', 'nexdirectory'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($boost_history_rows as $history_row) : ?>
                                <?php
                                $history_status = sanitize_key((string) ($history_row['status'] ?? 'pending'));
                                $history_status_class = isset($status_class_map[$history_status]) ? (string) $status_class_map[$history_status] : 'draft';
                                $duration_days = absint((int) ($history_row['duration_days'] ?? 0));
                                $duration_label = $duration_days > 0
                                    ? sprintf(_n('%d day', '%d days', $duration_days, 'nexdirectory'), $duration_days)
                                    : __('N/A', 'nexdirectory');
                                if (!empty($history_row['is_renewal'])) {
                                    $duration_label .= ' - ' . __('Renewal', 'nexdirectory');
                                }
                                ?>
                                <tr>
                                    <td data-label="<?php esc_attr_e('Listing', 'nexdirectory'); ?>">
                                        <a href="<?php echo esc_url((string) ($history_row['post_url'] ?? '#')); ?>"><?php echo esc_html((string) ($history_row['title'] ?? '')); ?></a>
                                    </td>
                                    <td data-label="<?php esc_attr_e('Package', 'nexdirectory'); ?>"><?php echo esc_html($duration_label); ?></td>
                                    <td data-label="<?php esc_attr_e('Amount', 'nexdirectory'); ?>">
                                        <?php echo esc_html(sprintf('%s %s', sanitize_text_field((string) ($history_row['currency'] ?? $boost_currency)), number_format((float) ($history_row['total_amount'] ?? 0), 2))); ?>
                                    </td>
                                    <td data-label="<?php esc_attr_e('Gateway', 'nexdirectory'); ?>"><?php echo esc_html((string) ($history_row['gateway_label'] ?? __('N/A', 'nexdirectory'))); ?></td>
                                    <td data-label="<?php esc_attr_e('Status', 'nexdirectory'); ?>">
                                        <span class="eep-status-pill eep-status-<?php echo esc_attr($history_status_class); ?>">
                                            <?php echo esc_html((string) ($history_row['status_label'] ?? ucfirst((string) $history_status))); ?>
                                        </span>
                                    </td>
                                    <td data-label="<?php esc_attr_e('Ordered', 'nexdirectory'); ?>"><?php echo esc_html((string) ($history_row['created_label'] ?? '')); ?></td>
                                    <td data-label="<?php esc_attr_e('Expires', 'nexdirectory'); ?>"><?php echo esc_html((string) ($history_row['expires_label'] ?? '')); ?></td>
                                    <td data-label="<?php esc_attr_e('Auto-Renew', 'nexdirectory'); ?>">
                                        <?php echo !empty($history_row['auto_renew']) ? esc_html__('On', 'nexdirectory') : esc_html__('Off', 'nexdirectory'); ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php else : ?>
                <p><?php esc_html_e('No boost orders yet.', 'nexdirectory'); ?></p>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
