<?php
namespace admin\Blocks\Render;

use WP_Query;

class Forum_Ajax {
	public function render_forum_ajax( $attributes, $content ) {
		// Upsell logic for frontend when Pro is not active.
		if ( ! class_exists( 'Forumax_Pro' ) ) {
			$upsell_img  = defined( 'FORUMAX_IMG' ) ? FORUMAX_IMG . 'upsell-forum-ajax.png' : '';
			$upgrade_url = admin_url( 'admin.php?page=forumax-pricing' );

			ob_start();
			?>
			<style>
				.bbpc-upsell-container {
					position: relative;
					display: flex;
					justify-content: center;
					align-items: center;
					min-height: 300px;
					background: #f5f5f5;
					border: 1px dashed #ccc;
					overflow: hidden;
					border-radius: 8px;
					margin: 20px 0;
				}

				.bbpc-upsell-container img {
					max-width: 100%;
					height: auto;
					display: block;
					opacity: 0.5;
				}

				.bbpc-upsell-btn-wrapper {
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					z-index: 10;
				}

				.bbpc-upgrade-btn-frontend {
					background: linear-gradient(90deg, #172473 0%, #0fa2d0 100%) !important;
					border: none !important;
					color: #fff !important;
					padding: 15px 20px !important;
					font-size: 18px !important;
					font-weight: 500 !important;
					border-radius: 4px !important;
					text-decoration: none !important;
					box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2) !important;
					transition: all 0.3s ease !important;
					display: inline-block !important;
					line-height: 1 !important;
				}

				.bbpc-upgrade-btn-frontend:hover {
					transform: translate(-2px, -2px);
					box-shadow: 0 6px 15px rgba(0, 0, 0, 0.3) !important;
					color: #fff !important;
					background: linear-gradient(80deg, #0fa2d0 0%, #172473 100%) !important;
				}
			</style>
			<div class="bbpc-upsell-container">
				<?php if ( $upsell_img ) : ?>
					<img src="<?php echo esc_url( $upsell_img ); ?>" alt="<?php esc_attr_e( 'Upgrade to Pro', 'bbp-core' ); ?>">
				<?php endif; ?>
				<div class="bbpc-upsell-btn-wrapper">
					<a href="<?php echo esc_url( $upgrade_url ); ?>" class="bbpc-upgrade-btn-frontend">
						<?php esc_html_e( 'Upgrade to Pro', 'bbp-core' ); ?>
					</a>
				</div>
			</div>
			<?php
			return ob_get_clean();
		}

		try {
			if ( ! wp_style_is( 'bbpc-el-widgets', 'registered' ) ) {
				if ( defined( 'FORUMAX_FRONT_ASS' ) ) {
					wp_register_style( 'bbpc-el-widgets', FORUMAX_STYLES . 'frontend/el-widgets.css' );
				}
			}

			wp_enqueue_style( 'bbpc-el-widgets' );

			// Extract settings with defaults
			$ppp2                    = isset( $attributes['ppp2'] ) ? absint( $attributes['ppp2'] ) : 9;
			$order                   = isset( $attributes['order'] ) ? $attributes['order'] : 'ASC';
			$orderby                 = isset( $attributes['orderby'] ) ? $attributes['orderby'] : 'date';
			$filter_btns             = isset( $attributes['filter_btns'] ) ? (bool) $attributes['filter_btns'] : true;
			$show_filter_all         = isset( $attributes['show_filter_all'] ) ? (bool) $attributes['show_filter_all'] : true;
			$show_filter_popular     = isset( $attributes['show_filter_popular'] ) ? (bool) $attributes['show_filter_popular'] : true;
			$show_filter_featured    = isset( $attributes['show_filter_featured'] ) ? (bool) $attributes['show_filter_featured'] : true;
			$show_filter_recent      = isset( $attributes['show_filter_recent'] ) ? (bool) $attributes['show_filter_recent'] : true;
			$show_filter_unloved     = isset( $attributes['show_filter_unloved'] ) ? (bool) $attributes['show_filter_unloved'] : true;
			$show_filter_loved       = isset( $attributes['show_filter_loved'] ) ? (bool) $attributes['show_filter_loved'] : true;
			$label_all               = ! empty( $attributes['label_all'] ) ? $attributes['label_all'] : __( 'All', 'bbp-core' );
			$label_popular           = ! empty( $attributes['label_popular'] ) ? $attributes['label_popular'] : __( 'Popular', 'bbp-core' );
			$label_featured          = ! empty( $attributes['label_featured'] ) ? $attributes['label_featured'] : __( 'Featured', 'bbp-core' );
			$label_recent            = ! empty( $attributes['label_recent'] ) ? $attributes['label_recent'] : __( 'Recent', 'bbp-core' );
			$label_unloved           = ! empty( $attributes['label_unloved'] ) ? $attributes['label_unloved'] : __( 'Unloved', 'bbp-core' );
			$label_loved             = ! empty( $attributes['label_loved'] ) ? $attributes['label_loved'] : __( 'Loved', 'bbp-core' );
			$show_views              = isset( $attributes['show_views'] ) ? (bool) $attributes['show_views'] : true;
			$show_likes              = isset( $attributes['show_likes'] ) ? (bool) $attributes['show_likes'] : true;
			$show_replies            = isset( $attributes['show_replies'] ) ? (bool) $attributes['show_replies'] : true;
			$show_author             = isset( $attributes['show_author'] ) ? (bool) $attributes['show_author'] : true;
			$show_time               = isset( $attributes['show_time'] ) ? (bool) $attributes['show_time'] : true;
			$show_parent_forum       = isset( $attributes['show_parent_forum'] ) ? (bool) $attributes['show_parent_forum'] : true;
			$enable_card_hover       = isset( $attributes['enable_card_hover'] ) ? (bool) $attributes['enable_card_hover'] : true;
			$no_posts_message        = ! empty( $attributes['no_posts_message'] ) ? $attributes['no_posts_message'] : __( 'No topics found.', 'bbp-core' );
			$hide_reach_mobile       = isset( $attributes['hide_reach_mobile'] ) ? (bool) $attributes['hide_reach_mobile'] : false;
			$forum_filter_ids        = ! empty( $attributes['forum_filter_ids'] ) ? $attributes['forum_filter_ids'] : '';
			$exclude_forum_ids       = ! empty( $attributes['exclude_forum_ids'] ) ? $attributes['exclude_forum_ids'] : '';
			$show_excerpt            = isset( $attributes['show_excerpt'] ) ? (bool) $attributes['show_excerpt'] : false;
			$excerpt_length          = isset( $attributes['excerpt_length'] ) ? absint( $attributes['excerpt_length'] ) : 20;
			$show_avatar             = isset( $attributes['show_avatar'] ) ? (bool) $attributes['show_avatar'] : false;
			$show_status_badge       = isset( $attributes['show_status_badge'] ) ? (bool) $attributes['show_status_badge'] : false;
			$load_more_enabled       = isset( $attributes['load_more'] ) ? (bool) $attributes['load_more'] : false;
			$load_more_text          = ! empty( $attributes['load_more_text'] ) ? $attributes['load_more_text'] : __( 'Load More', 'bbp-core' );

			// CSS sanitization helper
			$sanitize_css_color = function ( $color ) {
				$color = trim( (string) $color );
				if ( empty( $color ) ) {
					return '';
				}
				if ( preg_match( '/^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$/', $color ) ) {
					return $color;
				}
				if ( preg_match( '/^rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+(?:\s*,\s*(0|1|0?\.\d+))?\s*\)$/', $color ) ) {
					return $color;
				}
				return '';
			};

			// Sanitize color attributes.
			$forum_title_color        = $sanitize_css_color( isset( $attributes['forum_title_color'] ) ? $attributes['forum_title_color'] : '' );
			$forum_title_hover_color  = $sanitize_css_color( isset( $attributes['forum_title_hover_color'] ) ? $attributes['forum_title_hover_color'] : '' );
			$forum_meta_color         = $sanitize_css_color( isset( $attributes['forum_meta_color'] ) ? $attributes['forum_meta_color'] : '' );
			$parent_forum_color       = $sanitize_css_color( isset( $attributes['parent_forum_color'] ) ? $attributes['parent_forum_color'] : '' );
			$parent_forum_color_hover = $sanitize_css_color( isset( $attributes['parent_forum_color_hover'] ) ? $attributes['parent_forum_color_hover'] : '' );
			$card_bg_color            = $sanitize_css_color( isset( $attributes['card_bg_color'] ) ? $attributes['card_bg_color'] : '' );
			$filter_bar_bg_color      = $sanitize_css_color( isset( $attributes['filter_bar_bg_color'] ) ? $attributes['filter_bar_bg_color'] : '' );
			$active_filter_color      = $sanitize_css_color( isset( $attributes['active_filter_color'] ) ? $attributes['active_filter_color'] : '' );
			$reach_color              = $sanitize_css_color( isset( $attributes['reach_color'] ) ? $attributes['reach_color'] : '' );

			$unique_id = uniqid( 'bbpc-forum-' );

			// Check constants.
			if ( ! defined( 'FORUMAX_VEND' ) || ! defined( 'FORUMAX_FRONT_ASS' ) || ! defined( 'FORUMAX_VERSION' ) ) {
				return '<div class="alert alert-warning">' . esc_html__( 'Required constants missing.', 'bbp-core' ) . '</div>';
			}

			// Enqueue styles/scripts.
			if ( ! wp_style_is( 'elegant-icon', 'registered' ) ) {
				wp_register_style( 'elegant-icon', FORUMAX_VEND . '/elegant-icon/style.css' );
			}
			if ( ! wp_script_is( 'bbpc-ajax', 'registered' ) ) {
				wp_register_script( 'bbpc-ajax', FORUMAX_FRONT_ASS . 'js/ajax.js', array( 'jquery' ), FORUMAX_VERSION, true );
			}

			wp_enqueue_style( 'elegant-icon' );
			wp_enqueue_script( 'bbpc-ajax' );

			// Inline Styles
			$styles = '';
			$id     = '#' . $unique_id;

			if ( $forum_title_color ) {
				$styles .= "{$id} .single-forum-post-widget .post-title a { color: {$forum_title_color} !important; }";
			}
			if ( $forum_title_hover_color ) {
				$styles .= "{$id} .single-forum-post-widget .post-title a:hover { color: {$forum_title_hover_color} !important; }";
				$styles .= "{$id} .single-forum-post-widget:hover .post-title a { color: {$forum_title_hover_color} !important; }";
			}
			if ( $forum_meta_color ) {
				$styles .= "{$id} .single-forum-post-widget .post-info .author, {$id} .single-forum-post-widget .post-info .post-time { color: {$forum_meta_color} !important; }";
			}
			if ( $parent_forum_color ) {
				$styles .= "{$id} .post-content .post-category a { color: {$parent_forum_color} !important; }";
			}
			if ( $parent_forum_color_hover ) {
				$styles .= "{$id} .post-content .post-category a:hover { color: {$parent_forum_color_hover} !important; }";
			}
			if ( $card_bg_color ) {
				$styles .= "{$id} .single-forum-post-widget { background-color: {$card_bg_color} !important; }";
			}
			if ( $filter_bar_bg_color ) {
				$styles .= "{$id} .post-filter-widget { background-color: {$filter_bar_bg_color} !important; }";
			}
			if ( $active_filter_color ) {
				$styles .= "{$id} .single-filter-item a.data-active { color: {$active_filter_color} !important; }";
			}
			if ( $reach_color ) {
				$styles .= "{$id} .post-reach > div { color: {$reach_color} !important; }";
			}

			// Typography.
			if ( ! empty( $attributes['title_font_size'] ) ) {
				$size    = absint( $attributes['title_font_size'] );
				$styles .= "{$id} .single-forum-post-widget .post-title a { font-size: {$size}px !important; }";
			}
			if ( ! empty( $attributes['meta_font_size'] ) ) {
				$size    = absint( $attributes['meta_font_size'] );
				$styles .= "{$id} .single-forum-post-widget .post-info > div { font-size: {$size}px !important; }";
			}

			// Spacing.
			if ( ! empty( $attributes['card_border_radius'] ) ) {
				$radius  = absint( $attributes['card_border_radius'] );
				$styles .= "{$id} .single-forum-post-widget { border-radius: {$radius}px !important; }";
			}
			if ( ! empty( $attributes['card_gap'] ) ) {
				$gap     = absint( $attributes['card_gap'] );
				$styles .= "{$id} .single-forum-post-widget { margin-top: {$gap}px !important; }";
			}

			// Card hover disabled.
			if ( ! $enable_card_hover ) {
				$styles .= "{$id} .single-forum-post-widget:hover { transform: none !important; box-shadow: 0 1.6px 3.5px 0 #334d7226, 0 0.5px 1px 0 #334d721a !important; }";
				$styles .= "{$id} .single-forum-post-widget:hover .post-title h6 a { color: inherit !important; }";
			}

			// Responsive.
			if ( $hide_reach_mobile ) {
				$styles .= "@media (max-width: 768px) { {$id} .single-forum-post-widget .post-reach { display: none !important; } }";
			}

			// Status badge styles.
			if ( $show_status_badge ) {
				$styles .= "{$id} .frmx-status-badge { display: inline-block !important; padding: 2px 10px !important; border-radius: 12px !important; font-size: 11px !important; font-weight: 600 !important; line-height: 1.5 !important; margin-left: 8px !important; vertical-align: middle !important; }";
				$styles .= "{$id} .frmx-status-badge--open { background-color: #dcfce7 !important; color: #166534 !important; }";
				$styles .= "{$id} .frmx-status-badge--closed { background-color: #fee2e2 !important; color: #991b1b !important; }";
				$styles .= "{$id} .frmx-status-badge--resolved { background-color: #dbeafe !important; color: #1e40af !important; }";
			}

			// Excerpt styles.
			if ( $show_excerpt ) {
				$styles .= "{$id} .frmx-topic-excerpt { font-size: 13px !important; color: #6b7280 !important; margin-top: 6px !important; line-height: 1.5 !important; }";
			}

			// Load More button styles.
			if ( $load_more_enabled ) {
				$styles .= "{$id} .frmx-load-more-wrap { text-align: center !important; margin-top: 20px !important; }";
				$styles .= "{$id} .frmx-load-more-btn { display: inline-block !important; padding: 10px 28px !important; background: #3b82f6 !important; color: #fff !important; border: none !important; border-radius: 6px !important; font-size: 14px !important; font-weight: 500 !important; cursor: pointer !important; transition: background 0.3s ease !important; text-decoration: none !important; }";
				$styles .= "{$id} .frmx-load-more-btn:hover { background: #2563eb !important; }";
			}

			// Query
			$query_args = array(
				'post_type'      => 'topic',
				'posts_per_page' => $load_more_enabled ? -1 : $ppp2,
				'order'          => $order,
				'orderby'        => $orderby,
			);

			// Forum ID filter (include).
			if ( ! empty( $forum_filter_ids ) ) {
				$include_ids = array_map( 'absint', array_filter( explode( ',', $forum_filter_ids ) ) );
				if ( ! empty( $include_ids ) ) {
					$query_args['meta_query'][] = array(
						'key'     => '_bbp_forum_id',
						'value'   => $include_ids,
						'compare' => 'IN',
						'type'    => 'NUMERIC',
					);
				}
			}

			// Forum ID filter (exclude).
			if ( ! empty( $exclude_forum_ids ) ) {
				$exclude_ids = array_map( 'absint', array_filter( explode( ',', $exclude_forum_ids ) ) );
				if ( ! empty( $exclude_ids ) ) {
					$query_args['meta_query'][] = array(
						'key'     => '_bbp_forum_id',
						'value'   => $exclude_ids,
						'compare' => 'NOT IN',
						'type'    => 'NUMERIC',
					);
				}
			}

			$topics = new WP_Query( $query_args );

			$wrapper_attributes = get_block_wrapper_attributes( array(
				'class' => 'forum-post-widget',
				'id'    => $unique_id,
			) );

			$total_topics = $topics->post_count;

			ob_start();

			// Output styles.
			if ( ! empty( $styles ) ) {
				echo '<style>' . $styles . '</style>';
			}
			?>

			<div <?php echo $wrapper_attributes; ?> data_id="<?php echo esc_attr( $unique_id ); ?>">

				<?php if ( $filter_btns ) : ?>
					<div class="post-filter-widget mb-20">
						<?php if ( $show_filter_all ) : ?>
							<div class="single-filter-item">
								<a href="#" id="all_filt" data-forum="all" class="data-active">
									<i class="icon_grid-2x2"></i><?php echo esc_html( $label_all ); ?>
								</a>
							</div>
						<?php endif; ?>
						<?php if ( $show_filter_popular ) : ?>
							<div class="single-filter-item">
								<a href="#" id="populer_filt" data-forum="popular">
									<i class="icon_easel"></i><?php echo esc_html( $label_popular ); ?>
								</a>
							</div>
						<?php endif; ?>
						<?php if ( $show_filter_featured ) : ?>
							<div class="single-filter-item">
								<a href="#" id="featured_filt" data-forum="featured">
									<i class="icon_ribbon_alt"></i><?php echo esc_html( $label_featured ); ?>
								</a>
							</div>
						<?php endif; ?>
						<?php if ( $show_filter_recent ) : ?>
							<div class="single-filter-item">
								<a href="#" id="recent_filt" data-forum="recent">
									<i class="icon_clock_alt"></i><?php echo esc_html( $label_recent ); ?>
								</a>
							</div>
						<?php endif; ?>
						<?php if ( $show_filter_unloved ) : ?>
							<div class="single-filter-item">
								<a href="#" id="unloved_filt" data-forum="unloved">
									<i class="icon_close_alt2"></i><?php echo esc_html( $label_unloved ); ?>
								</a>
							</div>
						<?php endif; ?>
						<?php if ( $show_filter_loved ) : ?>
							<div class="single-filter-item">
								<a href="#" id="loved_filt" data-forum="loved">
									<i class="icon_check_alt2"></i><?php echo esc_html( $label_loved ); ?>
								</a>
							</div>
						<?php endif; ?>
					</div>
				<?php endif; ?>

				<div id="aj-post-filter-widget">
					<?php
					if ( $topics->have_posts() ) :
						$i = 0;
						while ( $topics->have_posts() ) :
							$topics->the_post();
							$topic_id   = $topics->posts[ $i ]->ID;
							$vote_count = get_post_meta( $topic_id, 'bbpv-votes', true );

							// Safe get forum ID.
							$forum_id = function_exists( 'bbp_get_topic_forum_id' ) ? bbp_get_topic_forum_id() : 0;
							// Safe get forum title.
							$forum_title = '';
							if ( function_exists( 'bbp_get_topic_forum_title' ) ) {
								$forum_title = bbp_get_topic_forum_title();
							} elseif ( function_exists( 'bbp_get_forum_title' ) ) {
								$forum_title = bbp_get_forum_title( $forum_id );
							}

							$hover_class = $enable_card_hover ? ' wow fadeInUp' : '';
							?>
							<div class="single-forum-post-widget<?php echo esc_attr( $hover_class ); ?>"
								<?php if ( $load_more_enabled && $i >= $ppp2 ) : ?> style="display: none !important;" data-frmx-hidden="1"<?php endif; ?>>
								<div class="post-content">
									<div class="post-title">
										<h6>
											<a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a>
											<?php if ( $show_status_badge ) :
												$topic_status = get_post_status( $topic_id );
												$is_resolved  = get_post_meta( $topic_id, '_bbp_topic_status', true );
												if ( 'resolved' === $is_resolved ) :
											?>
												<span class="frmx-status-badge frmx-status-badge--resolved"><?php esc_html_e( 'Resolved', 'bbp-core' ); ?></span>
											<?php elseif ( 'closed' === $topic_status ) : ?>
												<span class="frmx-status-badge frmx-status-badge--closed"><?php esc_html_e( 'Closed', 'bbp-core' ); ?></span>
											<?php else : ?>
												<span class="frmx-status-badge frmx-status-badge--open"><?php esc_html_e( 'Open', 'bbp-core' ); ?></span>
											<?php endif; endif; ?>
										</h6>
									</div>
									<?php if ( $show_excerpt ) :
										$raw_content = get_the_content( null, false, $topic_id );
										$excerpt     = wp_trim_words( wp_strip_all_tags( $raw_content ), $excerpt_length, '&hellip;' );
									?>
										<p class="frmx-topic-excerpt"><?php echo esc_html( $excerpt ); ?></p>
									<?php endif; ?>
									<div class="post-info">
										<?php if ( $show_author ) : ?>
											<div class="author">
												<?php
												$author_id = get_post_field( 'post_author', $topic_id );
												if ( $show_avatar && $author_id ) {
													echo get_avatar( $author_id, 24 );
												} elseif ( defined( 'FORUMAX_IMG' ) ) {
												?>
													<img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/user-circle-alt.svg' ); ?>"
														alt="<?php esc_attr_e( 'User icon', 'bbp-core' ); ?>">
												<?php } ?>
												<?php
												if ( function_exists( 'bbp_get_topic_author_link' ) ) {
													echo wp_kses_post( bbp_get_topic_author_link(
														array(
															'post_id' => $topic_id,
															'type'    => 'name',
														)
													) );
												}
												?>
											</div>
										<?php endif; ?>

										<?php if ( $show_time ) : ?>
											<div class="post-time">
												<?php if ( defined( 'FORUMAX_IMG' ) ) : ?>
													<img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/time-outline.svg' ); ?>"
														alt="<?php esc_attr_e( 'Time icon', 'bbp-core' ); ?>">
												<?php endif; ?>
												<?php
												if ( function_exists( 'bbp_topic_last_active_time' ) ) {
													bbp_topic_last_active_time( get_the_ID() );
												} elseif ( function_exists( 'bbp_forum_last_active_time' ) ) {
													bbp_forum_last_active_time( get_the_ID() );
												}
												?>
											</div>
										<?php endif; ?>
									</div>

									<?php if ( $show_parent_forum ) : ?>
										<div class="post-category">
											<a href="<?php echo esc_url( get_the_permalink( $forum_id ) ); ?>">
												<?php echo get_the_post_thumbnail( $forum_id ); ?>
												<?php echo esc_html( $forum_title ); ?>
											</a>
										</div>
									<?php endif; ?>
								</div>

								<?php if ( $show_views || $show_likes || $show_replies ) : ?>
									<div class="post-reach">
										<?php if ( $show_views ) : ?>
											<div class="post-view">
												<?php if ( defined( 'FORUMAX_IMG' ) ) : ?>
													<img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/eye-outline.svg' ); ?>"
														alt="<?php esc_attr_e( 'Views icon', 'bbp-core' ); ?>">
												<?php endif; ?>
												<?php
												if ( function_exists( 'forumax_topic_view_count' ) ) {
													forumax_topic_view_count( $topic_id );
												}
												echo '&nbsp;';
												esc_html_e( 'Views', 'bbp-core' );
												?>
											</div>
										<?php endif; ?>

										<?php if ( $show_likes ) : ?>
											<div class="post-like">
												<?php if ( defined( 'FORUMAX_IMG' ) ) : ?>
													<img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/thumbs-up-outline.svg' ); ?>"
														alt="<?php esc_attr_e( 'Likes icon', 'bbp-core' ); ?>">
												<?php endif; ?>
												<?php
												echo esc_html( $vote_count ? $vote_count : '0' );
												echo '&nbsp;';
												esc_html_e( 'Likes', 'bbp-core' );
												?>
											</div>
										<?php endif; ?>

										<?php if ( $show_replies ) : ?>
											<div class="post-comment">
												<?php if ( defined( 'FORUMAX_IMG' ) ) : ?>
													<img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/chatbubbles-outline.svg' ); ?>"
														alt="<?php esc_attr_e( 'Replies icon', 'bbp-core' ); ?>">
												<?php endif; ?>
												<?php
												if ( function_exists( 'bbp_topic_reply_count' ) ) {
													bbp_topic_reply_count( $topic_id );
												}
												echo '&nbsp;';
												esc_html_e( 'Replies', 'bbp-core' );
												?>
											</div>
										<?php endif; ?>
									</div>
								<?php endif; ?>
							</div>
							<?php
							$i++;
						endwhile;
						wp_reset_postdata();
					else :
						?>
						<div class="alert alert-warning"><?php echo esc_html( $no_posts_message ); ?></div>
					<?php endif; ?>
				</div>

				<?php if ( $load_more_enabled && $total_topics > $ppp2 ) : ?>
					<div class="frmx-load-more-wrap">
						<button type="button" class="frmx-load-more-btn"
							data-per-page="<?php echo esc_attr( $ppp2 ); ?>"
							data-total="<?php echo esc_attr( $total_topics ); ?>"
							data-container="#<?php echo esc_attr( $unique_id ); ?> #aj-post-filter-widget">
							<?php echo esc_html( $load_more_text ); ?>
						</button>
					</div>
				<?php endif; ?>

				<?php if ( $load_more_enabled && $total_topics > $ppp2 ) :
					$load_more_js = '
						jQuery(document).on("click", "#' . esc_js( $unique_id ) . ' .frmx-load-more-btn", function(e) {
							e.preventDefault();
							var $wrapper = jQuery("#' . esc_js( $unique_id ) . '");
							var perPage = parseInt(jQuery(this).attr("data-per-page"), 10) || 9;
							var $hidden = $wrapper.find("[data-frmx-hidden=1]");
							$hidden.slice(0, perPage).each(function() {
								jQuery(this).removeAttr("style").removeAttr("data-frmx-hidden");
							});
							if ($wrapper.find("[data-frmx-hidden=1]").length === 0) {
								jQuery(this).parent().hide();
							}
						});
					';
					wp_add_inline_script( 'bbpc-ajax', $load_more_js );
				endif; ?>
			</div>
			<?php
			return ob_get_clean();
		} catch ( \Throwable $e ) {
			return '<div>Error rendering block: ' . esc_html( $e->getMessage() ) . '</div>';
		}
	}
}