<?php
/**
 * Ajax Actions for forum filtering
 * @since 0.0.1
 */

defined( 'ABSPATH' ) || exit;

add_action( 'wp_ajax_forumax_ajax_forum', 'forumax_ajax_forum' );
add_action( 'wp_ajax_nopriv_forumax_ajax_forum', 'forumax_ajax_forum' );

/**
 * AJAX handler for forum filtering
 *
 * @return void
 */
function forumax_ajax_forum() {
	check_ajax_referer( 'forumax-nonce', 'nonce' );

	// received values from front end forum
	$data_forum = isset( $_POST['data_forum'] ) ? sanitize_text_field( $_POST['data_forum'] ) : '';

	$args = [
		'post_type'      => 'topic',
		'posts_per_page' => 9,
	];

	if ( 'solved' === $data_forum ) {
		$args['meta_query'] = [
			[
				'key'   => '_bbp_topic_is_solved',
				'value' => true,
			],
		];
	} elseif ( 'unsolved' === $data_forum ) {
		$args['meta_query'] = [
			[
				'key'     => '_bbp_topic_is_solved',
				'compare' => 'NOT EXISTS',
			],
		];
	} elseif ( 'recent' === $data_forum ) {
        $args['order'] = 'DESC';
	} elseif ( 'loved' === $data_forum ) {
		// Topics with positive vote counts (most loved)
		$args['meta_query'] = [
			[
				'key'     => 'bbpv-votes',
				'value'   => 0,
				'compare' => '>',
				'type'    => 'NUMERIC',
			],
		];
		$args['orderby'] = [
			'meta_value_num' => 'DESC',
			'comment_count'  => 'DESC',
		];
	} elseif ( 'unloved' === $data_forum ) {
		// Topics without positive votes (no votes or zero)
		$args['meta_query'] = [
			'relation' => 'OR',
			[
				'key'     => 'bbpv-votes',
				'compare' => 'NOT EXISTS',
			],
			[
				'key'     => 'bbpv-votes',
				'value'   => 0,
				'compare' => '=',
				'type'    => 'NUMERIC',
			],
		];
	} elseif ( 'popular' === $data_forum ) {
		$args['meta_query'] = [
			'relation' => 'OR',
			[
				'key'     => '_btv_view_count',
				'value'   => 0,
				'compare' => '>',
				'type'    => 'NUMERIC',
			],
			[
				'key'     => '_bbp_reply_count',
				'value'   => 0,
				'compare' => '>',
				'type'    => 'NUMERIC',
			],
			[
				'key'     => 'bbpv-votes',
				'value'   => 0,
				'compare' => '>',
				'type'    => 'NUMERIC',
			],
		];
		$args['orderby'] = [
			'meta_value_num' => 'DESC',
			'comment_count'  => 'DESC',
		];
	} elseif ( 'featured' === $data_forum ) {

		$super_stickies = bbp_get_super_stickies(); // Get global super sticky topics

		// Get sticky topics from all forums
		$stickies = [];
		$forums   = get_posts( [
			'post_type'      => 'forum',
			'posts_per_page' => -1,
			'fields'         => 'ids',
		] );

		foreach ( $forums as $forum_id ) {
			$stickies = array_merge( $stickies, bbp_get_stickies( $forum_id ) );
		}

		// Merge both sticky types
		$sticky_topics = array_merge( $super_stickies, $stickies );
		$sticky_topics = array_unique( $sticky_topics );

		if ( ! empty( $sticky_topics ) ) {
			$args['post__in'] = $sticky_topics;
		} else {
			wp_send_json_error( __( 'No featured (sticky) topics found', 'forumax' ) );
		}

	}

	// WP_Query arguments
	$forum_topics = new WP_Query( $args );

	if ( $forum_topics->have_posts() ) {
		$i = 0;
		while ( $forum_topics->have_posts() ):
			$forum_topics->the_post();
			$item_id    = get_the_ID();
			$author_id  = get_post_field( 'post_author', $item_id );
			$topic_id   = $forum_topics->posts[ $i ]->ID;
			$vote_count = get_post_meta( $topic_id, "bbpv-votes", true );
			$forum_id   = bbp_get_topic_forum_id();
			?>
            <div class="single-forum-post-widget">
                <div class="post-content">
                    <div class="post-title">
                        <h6><a href="<?php the_permalink(); ?>"> <?php the_title() ?> </a></h6>
                    </div>
                    <div class="post-info">
                        <div class="author">
                            <img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/user-circle-alt.svg' ); ?>" alt="<?php esc_attr_e( 'User circle icon', 'forumax' ); ?>">
							<?php 
                            echo wp_kses_post( bbp_get_topic_author_link( 
                                [
                                    'post_id' 	=> $topic_id, 
                                    'type' 		=> 'name' 
                                ]
                            ) );
                            ?>
                        </div>

                        <div class="post-time">
                            <img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/time-outline.svg' ); ?>"
                                 alt="<?php esc_attr_e( 'Time outline icon', 'forumax' ); ?>">
							<?php bbp_forum_last_active_time( get_the_ID() ); ?>
                        </div>
                    </div>

                    <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( get_the_title( $forum_id ) ); ?>
                        </a>
                    </div>
                </div>
                <div class="post-reach">
                    <div class="post-view">
                        <img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/eye-outline.svg' ); ?>"
                             alt="<?php esc_attr_e( 'Eye outline icon', 'forumax' ); ?>">

						<?php
						forumax_topic_view_count( $topic_id );
						echo '&nbsp;';
						esc_html_e( 'Views', 'forumax' );
						?>

                    </div>
                    <div class="post-like">
                        <img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/thumbs-up-outline.svg' ); ?>"
                             alt="<?php esc_attr_e( 'Thumbs up icon', 'forumax' ); ?>">

						<?php
						if ( $vote_count ) {
							echo esc_html( $vote_count );
						} else {
							echo "0";
						}

						echo '&nbsp;';
						esc_html_e( 'Likes', 'forumax' );
						?>
                    </div>
                    <div class="post-comment">
                        <img src="<?php echo esc_url( FORUMAX_IMG . 'forum_tab/chatbubbles-outline.svg' ); ?>"
                             alt="<?php esc_attr_e( 'Chatbubbles outline icon', 'forumax' ); ?>">

						<?php
						bbp_topic_reply_count( $topic_id );
						echo '&nbsp;';
						esc_html_e( 'Replies', 'forumax' );
						?>
                    </div>
                </div>
            </div>
			<?php
			$i ++;
		endwhile;
	} else {
		esc_html_e( 'no posts found', 'forumax' );
	}

	wp_reset_postdata();
	wp_die();
}
