<?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 = sanitize_key((string) ($profile_key ?? ($ui_labels['profile_key'] ?? '')));
$notifications_title = sanitize_text_field((string) ($notifications_title ?? __('Notifications & Inbox', 'nexdirectory')));
$notifications_description = sanitize_text_field((string) ($notifications_description ?? __('Review account activity and reply to inbox messages.', 'nexdirectory')));
$threads = isset($threads) && is_array($threads) ? $threads : [];
$active_thread_id = isset($active_thread_id) ? absint((int) $active_thread_id) : 0;
$active_thread = isset($active_thread) && is_array($active_thread) ? $active_thread : [];
$active_messages = isset($active_messages) && is_array($active_messages) ? $active_messages : [];
$active_subject = sanitize_text_field((string) ($active_subject ?? ''));
$active_event_title = sanitize_text_field((string) ($active_event_title ?? ''));
$active_event_url = isset($active_event_url) ? esc_url((string) $active_event_url) : '';
$active_other_name = sanitize_text_field((string) ($active_other_name ?? __('Member', 'nexdirectory')));
$reply_action = isset($reply_action) ? esc_url((string) $reply_action) : esc_url(admin_url('admin-post.php'));
$reply_nonce = sanitize_text_field((string) ($reply_nonce ?? ''));
$reply_subject = sanitize_text_field((string) ($reply_subject ?? $active_subject));
$unread_count = isset($unread_count) ? absint((int) $unread_count) : 0;
$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($notifications_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($notifications_title); ?></h1>
    <p><?php echo esc_html($notifications_description); ?></p>
</section>
<?php echo wp_kses((string) $this->render_account_tabs('notifications', $profile_key), NEXDIRECTORY_Escaping::allowed_html()); ?>
<div class="nexdirectory-account-shell">
    <section
        class="nexdirectory-notification-center nexdirectory-notification-center--page"
        data-nexdirectory-notification-center="page"
        aria-labelledby="nexdirectory-account-notifications-title"
    >
        <div class="nexdirectory-notification-center__header">
            <div>
                <p class="nexdirectory-notification-center__eyebrow"><?php esc_html_e('Activity feed', 'nexdirectory'); ?></p>
                <h2 id="nexdirectory-account-notifications-title"><?php esc_html_e('Latest notifications', 'nexdirectory'); ?></h2>
                <p class="nexdirectory-meta"><?php esc_html_e('Messages, claims, moderation, attendance updates and compatible extension notices appear here.', 'nexdirectory'); ?></p>
            </div>
            <button type="button" class="nexdirectory-notification-center__mark-all" data-nexdirectory-notification-mark-all disabled>
                <?php esc_html_e('Mark all as read', 'nexdirectory'); ?>
            </button>
        </div>
        <div
            class="nexdirectory-notification-center__list"
            data-nexdirectory-notification-list
            role="feed"
            aria-live="polite"
            aria-busy="true"
        ></div>
        <div class="nexdirectory-notification-center__empty" data-nexdirectory-notification-empty hidden>
            <span class="nexdirectory-notification-center__empty-icon" aria-hidden="true">✓</span>
            <strong><?php esc_html_e('You are all caught up', 'nexdirectory'); ?></strong>
            <span><?php esc_html_e('New account activity will appear here.', 'nexdirectory'); ?></span>
        </div>
    </section>

    <div class="nexdirectory-inbox-section-heading">
        <div>
            <p class="nexdirectory-notification-center__eyebrow"><?php esc_html_e('Direct messages', 'nexdirectory'); ?></p>
            <h2><?php esc_html_e('Inbox', 'nexdirectory'); ?></h2>
        </div>
    </div>
    <div class="nexdirectory-inbox-shell" data-nexdirectory-inbox-shell="1">
        <aside class="nexdirectory-inbox-threads">
            <div class="nexdirectory-inbox-threads-head">
                <h3><?php esc_html_e('Conversations', 'nexdirectory'); ?></h3>
                <span class="nexdirectory-status-pill <?php echo $unread_count > 0 ? 'nexdirectory-status-pending' : 'nexdirectory-status-draft'; ?>">
                    <?php
                    echo esc_html(sprintf(
                        /* translators: 1: Placeholder value 1, 2: Placeholder value 2. */
                        _n('%d unread', '%d unread', $unread_count, 'nexdirectory'),
                        $unread_count
                    ));
                    ?>
                </span>
            </div>
            <?php if (!empty($threads)) : ?>
                <div class="nexdirectory-inbox-thread-list" data-nexdirectory-inbox-thread-list="1">
                    <?php foreach ($threads as $thread_row) : ?>
                        <?php
                        $thread_row_id = absint((int) ($thread_row['thread_id'] ?? 0));
                        $thread_subject = sanitize_text_field((string) ($thread_row['subject'] ?? ''));
                        if ($thread_subject === '') {
                            $thread_subject = sanitize_text_field((string) ($thread_row['event_title'] ?? __('Conversation', 'nexdirectory')));
                        }
                        $thread_url = esc_url((string) ($thread_row['thread_url'] ?? '#'));
                        $other_name = sanitize_text_field((string) ($thread_row['other_name'] ?? __('Member', 'nexdirectory')));
                        $last_body = sanitize_text_field((string) ($thread_row['last_body'] ?? ''));
                        $last_time = sanitize_text_field((string) ($thread_row['last_time'] ?? ''));
                        $unread = absint((int) ($thread_row['unread_count'] ?? 0));
                        ?>
                        <a class="nexdirectory-inbox-thread-item<?php echo $thread_row_id === $active_thread_id ? ' is-active' : ''; ?>" data-nexdirectory-inbox-thread-link="1" data-nexdirectory-thread-id="<?php echo esc_attr((string) $thread_row_id); ?>" href="<?php echo esc_url($thread_url); ?>">
                            <strong><?php echo esc_html($thread_subject); ?></strong>
                            <?php /* translators: %s: Placeholder value. */ ?>
                            <p class="nexdirectory-meta"><?php echo esc_html(sprintf(__('With %s', 'nexdirectory'), $other_name)); ?></p>
                            <?php if ($last_body !== '') : ?>
                                <p class="nexdirectory-meta nexdirectory-inbox-thread-preview"><?php echo esc_html($last_body); ?></p>
                            <?php endif; ?>
                            <div class="nexdirectory-inbox-thread-foot">
                                <span class="nexdirectory-meta"><?php echo esc_html($last_time); ?></span>
                                <?php if ($unread > 0) : ?>
                                    <span class="nexdirectory-status-pill nexdirectory-status-pending"><?php echo esc_html((string) $unread); ?></span>
                                <?php endif; ?>
                            </div>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php else : ?>
                <p class="nexdirectory-meta"><?php esc_html_e('No messages yet. Contact forms and attendee updates appear here.', 'nexdirectory'); ?></p>
            <?php endif; ?>
        </aside>

        <section class="nexdirectory-inbox-panel" data-nexdirectory-inbox-panel="1">
            <?php if (!empty($active_thread)) : ?>
                <header class="nexdirectory-inbox-panel-head">
                    <div>
                        <h3><?php echo esc_html($active_subject !== '' ? $active_subject : __('Conversation', 'nexdirectory')); ?></h3>
                        <?php /* translators: %s: Placeholder value. */ ?>
                        <p class="nexdirectory-meta"><?php echo esc_html(sprintf(__('With %s', 'nexdirectory'), $active_other_name)); ?></p>
                        <?php if ($active_event_title !== '' && $active_event_url !== '') : ?>
                            <p class="nexdirectory-meta"><a href="<?php echo esc_url($active_event_url); ?>"><?php echo esc_html($active_event_title); ?></a></p>
                        <?php endif; ?>
                    </div>
                </header>

                <div class="nexdirectory-inbox-messages" data-nexdirectory-inbox-messages>
                    <?php foreach ($active_messages as $message_row) : ?>
                        <?php
                        $message_id = absint((int) ($message_row['message_id'] ?? 0));
                        $sender_name = sanitize_text_field((string) ($message_row['sender_name'] ?? __('Member', 'nexdirectory')));
                        $subject = sanitize_text_field((string) ($message_row['subject'] ?? ''));
                        $body = nexdirectory_sanitize_textarea((string) ($message_row['body'] ?? ''));
                        $attachment_url = esc_url((string) ($message_row['attachment_url'] ?? ''));
                        $created_label = sanitize_text_field((string) ($message_row['created_label'] ?? ''));
                        $is_mine = !empty($message_row['is_mine']);
                        $is_highlight = !empty($message_row['is_highlight']);
                        ?>
                        <article
                            class="nexdirectory-inbox-message<?php echo $is_mine ? ' nexdirectory-inbox-message-own' : ''; ?><?php echo $is_highlight ? ' nexdirectory-inbox-message-highlight' : ''; ?>"
                            data-nexdirectory-message-id="<?php echo esc_attr((string) $message_id); ?>"
                        >
                            <div class="nexdirectory-inbox-message-meta">
                                <strong><?php echo esc_html($is_mine ? __('You', 'nexdirectory') : $sender_name); ?></strong>
                                <span><?php echo esc_html($created_label); ?></span>
                            </div>
                            <?php if ($subject !== '') : ?>
                                <p class="nexdirectory-inbox-message-subject"><?php echo esc_html($subject); ?></p>
                            <?php endif; ?>
                            <div class="nexdirectory-inbox-message-body"><?php echo nl2br(esc_html($body)); ?></div>
                            <?php if ($attachment_url !== '') : ?>
                                <p class="nexdirectory-inbox-message-attachment">
                                    <a href="<?php echo esc_url($attachment_url); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e('View attachment', 'nexdirectory'); ?></a>
                                </p>
                            <?php endif; ?>
                        </article>
                    <?php endforeach; ?>
                </div>

                <form method="post" action="<?php echo esc_url($reply_action); ?>" class="nexdirectory-form nexdirectory-inbox-reply-form" data-nexdirectory-inbox-reply-form="1">
                    <input type="hidden" name="action" value="nexdirectory_inbox_reply">
                    <input type="hidden" name="thread_id" value="<?php echo esc_attr((string) $active_thread_id); ?>">
                    <input type="hidden" name="nexdirectory_inbox_reply_nonce" value="<?php echo esc_attr($reply_nonce); ?>">
                    <input type="hidden" name="redirect_to" value="<?php echo esc_attr($this->build_inbox_thread_url($active_thread_id, 0)); ?>">
                    <label for="nexdirectory-inbox-reply-subject"><?php esc_html_e('Subject', 'nexdirectory'); ?></label>
                    <input id="nexdirectory-inbox-reply-subject" type="text" name="subject" value="<?php echo esc_attr($reply_subject); ?>" maxlength="160">
                    <label for="nexdirectory-inbox-reply-message"><?php esc_html_e('Reply', 'nexdirectory'); ?></label>
                    <textarea id="nexdirectory-inbox-reply-message" name="message" rows="4" required placeholder="<?php esc_attr_e('Write your reply...', 'nexdirectory'); ?>"></textarea>
                    <button type="submit" class="nexdirectory-btn nexdirectory-btn-sm"><?php esc_html_e('Send Reply', 'nexdirectory'); ?></button>
                </form>
            <?php else : ?>
                <div class="nexdirectory-empty-state nexdirectory-inbox-empty">
                    <h3><?php esc_html_e('No conversation selected', 'nexdirectory'); ?></h3>
                    <p><?php esc_html_e('Choose a conversation from the left to read and reply.', 'nexdirectory'); ?></p>
                </div>
            <?php endif; ?>
        </section>
    </div>
</div>
