<?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'] ?? '')));
$notifications_title = sanitize_text_field((string) ($notifications_title ?? __('Inbox', 'nexdirectory')));
$notifications_description = sanitize_text_field((string) ($notifications_description ?? __('View 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 $this->render_notice();
echo $this->render_platform_breadcrumbs($notifications_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($notifications_title); ?></h1>
    <p><?php echo esc_html($notifications_description); ?></p>
</section>
<?php echo $this->render_account_tabs('notifications', $profile_key); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div class="eep-account-shell">
    <div class="eep-inbox-shell" data-eep-inbox-shell="1">
        <aside class="eep-inbox-threads">
            <div class="eep-inbox-threads-head">
                <h3><?php esc_html_e('Conversations', 'nexdirectory'); ?></h3>
                <span class="eep-status-pill <?php echo $unread_count > 0 ? 'eep-status-pending' : 'eep-status-draft'; ?>">
                    <?php
                    echo esc_html(sprintf(
                        _n('%d unread', '%d unread', $unread_count, 'nexdirectory'),
                        $unread_count
                    ));
                    ?>
                </span>
            </div>
            <?php if (!empty($threads)) : ?>
                <div class="eep-inbox-thread-list" data-eep-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="eep-inbox-thread-item<?php echo $thread_row_id === $active_thread_id ? ' is-active' : ''; ?>" data-eep-inbox-thread-link="1" data-eep-thread-id="<?php echo esc_attr((string) $thread_row_id); ?>" href="<?php echo $thread_url; ?>">
                            <strong><?php echo esc_html($thread_subject); ?></strong>
                            <p class="eep-meta"><?php echo esc_html(sprintf(__('With %s', 'nexdirectory'), $other_name)); ?></p>
                            <?php if ($last_body !== '') : ?>
                                <p class="eep-meta eep-inbox-thread-preview"><?php echo esc_html($last_body); ?></p>
                            <?php endif; ?>
                            <div class="eep-inbox-thread-foot">
                                <span class="eep-meta"><?php echo esc_html($last_time); ?></span>
                                <?php if ($unread > 0) : ?>
                                    <span class="eep-status-pill eep-status-pending"><?php echo esc_html((string) $unread); ?></span>
                                <?php endif; ?>
                            </div>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php else : ?>
                <p class="eep-meta"><?php esc_html_e('No messages yet. Contact forms and attendee updates appear here.', 'nexdirectory'); ?></p>
            <?php endif; ?>
        </aside>

        <section class="eep-inbox-panel" data-eep-inbox-panel="1">
            <?php if (!empty($active_thread)) : ?>
                <header class="eep-inbox-panel-head">
                    <div>
                        <h3><?php echo esc_html($active_subject !== '' ? $active_subject : __('Conversation', 'nexdirectory')); ?></h3>
                        <p class="eep-meta"><?php echo esc_html(sprintf(__('With %s', 'nexdirectory'), $active_other_name)); ?></p>
                        <?php if ($active_event_title !== '' && $active_event_url !== '') : ?>
                            <p class="eep-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="eep-inbox-messages" data-eep-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 = eep_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="eep-inbox-message<?php echo $is_mine ? ' eep-inbox-message-own' : ''; ?><?php echo $is_highlight ? ' eep-inbox-message-highlight' : ''; ?>"
                            data-eep-message-id="<?php echo esc_attr((string) $message_id); ?>"
                        >
                            <div class="eep-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="eep-inbox-message-subject"><?php echo esc_html($subject); ?></p>
                            <?php endif; ?>
                            <div class="eep-inbox-message-body"><?php echo nl2br(esc_html($body)); ?></div>
                            <?php if ($attachment_url !== '') : ?>
                                <p class="eep-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 $reply_action; ?>" class="eep-form eep-inbox-reply-form" data-eep-inbox-reply-form="1">
                    <input type="hidden" name="action" value="eep_inbox_reply">
                    <input type="hidden" name="thread_id" value="<?php echo esc_attr((string) $active_thread_id); ?>">
                    <input type="hidden" name="eep_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="eep-inbox-reply-subject"><?php esc_html_e('Subject', 'nexdirectory'); ?></label>
                    <input id="eep-inbox-reply-subject" type="text" name="subject" value="<?php echo esc_attr($reply_subject); ?>" maxlength="160">
                    <label for="eep-inbox-reply-message"><?php esc_html_e('Reply', 'nexdirectory'); ?></label>
                    <textarea id="eep-inbox-reply-message" name="message" rows="4" required placeholder="<?php esc_attr_e('Write your reply...', 'nexdirectory'); ?>"></textarea>
                    <button type="submit" class="eep-btn eep-btn-sm"><?php esc_html_e('Send Reply', 'nexdirectory'); ?></button>
                </form>
            <?php else : ?>
                <div class="eep-empty-state eep-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>
