<?php
/**
 * Single Topic Content Part
 *
 * @package bbPress
 * @subpackage Theme
 */

// Exit if accessed directly
defined('ABSPATH') || exit;

$has_sidebar = is_active_sidebar('forum_archive_sidebar');
$main_col_class = $has_sidebar ? 'frmx-col-lg-8' : 'frmx-col-lg-12';
?>
<section <?php bbp_topic_class(get_the_ID(), array('forum-single-content')); ?>>
    <div class="forum-layout">

        <div class="frmx-row frmx-sidebar-<?php echo esc_attr(forumax_get_opt('sidebar_position', 'right')); ?>">
            <div class="<?php echo esc_attr($main_col_class); ?> forum-main-column">

                <?php
                if (post_password_required()):
                    bbp_get_template_part('form', 'protected');
                else:
                    bbp_get_template_part('content', 'single-topic-lead');
                    ?>

                    <?php if (bbp_has_replies()): ?>

                        <div id="topic-<?php bbp_topic_id(); ?>-replies" class="all-answers">

                            <div class="all-replies-header frmx-all-replies-header">
                                <div class="frmx-header-left">
                                    <h5 class="replies-title">
                                        <svg class="frmx-reply-count-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
                                        <?php
                                        $reply_count = bbp_get_topic_reply_count( bbp_get_topic_id() );
                                        printf(
                                            /* translators: %s: number of replies */
                                            esc_html( _n( '%s Reply', '%s Replies', $reply_count, 'forumax' ) ),
                                            esc_html( number_format_i18n( $reply_count ) )
                                        );
                                        ?>
                                    </h5>

                                    <?php
                                    /**
                                     * Participant avatar stack.
                                     *
                                     * Fetches unique reply authors for this topic and
                                     * renders an overlapping avatar row for social context.
                                     */
                                    $topic_id     = bbp_get_topic_id();
                                    $max_avatars  = 6;
                                    $reply_ids    = get_posts( [
                                        'post_type'      => bbp_get_reply_post_type(),
                                        'post_parent'    => $topic_id,
                                        'post_status'    => 'publish',
                                        'posts_per_page' => 100,
                                        'fields'         => 'ids',
                                        'no_found_rows'  => true,
                                        'update_post_meta_cache' => false,
                                        'update_post_term_cache' => false,
                                    ] );

                                    $author_ids = [];
                                    foreach ( $reply_ids as $rid ) {
                                        $aid = (int) get_post_field( 'post_author', $rid );
                                        if ( $aid && ! in_array( $aid, $author_ids, true ) ) {
                                            $author_ids[] = $aid;
                                        }
                                    }

                                    $total_participants = count( $author_ids );

                                    if ( $total_participants > 0 ) :
                                        $shown_ids   = array_slice( $author_ids, 0, $max_avatars );
                                        $extra_count = $total_participants - $max_avatars;
                                    ?>
                                    <?php endif; ?>
                                </div>

                                <div class="frmx-header-right">
                                    <div class="replies-sorting">
                                        <select name="sort-replies" id="sort-replies" class="replies-sort-select">
                                            <option selected disabled>Sort By</option>
                                            <option value="desc">Newest First</option>
                                            <option value="asc">Oldest First</option>
                                            <option value="vote">Highest Votes First</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="reply-result">
                                <?php bbp_get_template_part('loop', 'replies'); ?>
                            </div>

                            <?php bbp_get_template_part('pagination', 'replies'); ?>

                        </div>

                    <?php endif; ?>

                    <?php bbp_get_template_part('form', 'reply'); ?>

                    <?php bbp_get_template_part('alert', 'topic-lock'); ?>

                    <?php do_action('bbp_template_after_single_topic'); ?>

                <?php endif; ?>

            </div>
            <!-- /.forum-main-column -->

            <?php if ($has_sidebar): ?>
                <div class="frmx-col-lg-4 forum-sidebar-column">
                    <aside class="forum-sidebar">
                        <?php dynamic_sidebar('forum_archive_sidebar'); ?>
                    </aside>
                </div>
            <?php endif; ?>
        </div>
    </div>
</section>