<?php
/**
 * Create Forum/Subforum functionality
 *
 * @package Forumax
 */

defined( 'ABSPATH' ) || exit;

// AJAX handler to create a forum
add_action( 'wp_ajax_forumax_create_forum', 'forumax_create_forum' );

/**
 * Create a new forum.
 */
function forumax_create_forum() {

	// Check the nonce
	check_ajax_referer( 'bbpc-admin-nonce', 'bbpc_nonce' );

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( 'Unauthorized request.' );
	}

	$bbp_forum_title = isset( $_POST['bbp_forum_title'] ) ? sanitize_text_field( wp_unslash( $_POST['bbp_forum_title'] ) ) : '';
	if ( empty( $bbp_forum_title ) ) {
		wp_send_json_error( 'Forum title cannot be empty.' );
	}

	$args = [
		'post_type'   => 'forum',
		'post_parent' => 0,
	];

	$query = new \WP_Query( $args );
	$total = $query->found_posts;
	$add   = 2;
	$order = $total + $add;

	// Insert the post
	$post_id = wp_insert_post( [
		'post_title'   => $bbp_forum_title,
		'post_parent'  => 0,
		'post_content' => '',
		'post_type'    => 'forum',
		'post_status'  => 'publish',
		'post_author'  => get_current_user_id(),
		'menu_order'   => $order,
	] );

	if ( is_wp_error( $post_id ) ) {
		wp_send_json_error( 'Failed to create the forum.' );
	}

	// Return success
	wp_send_json_success( 'Forum created successfully.' );
}

// AJAX handler to create a sub-forum
add_action( 'wp_ajax_forumax_create_subforum', 'forumax_create_subforum' );

/**
 * Create a sub-forum under a parent forum.
 *
 * @return void
 */
function forumax_create_subforum() {

	// Check the nonce.
	check_ajax_referer( 'bbpc-admin-nonce', 'bbpc_nonce' );

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( 'Unauthorized request.' );
	}

	$bbp_subforum_title = isset( $_POST['bbp_subforum_title'] ) ? sanitize_text_field( wp_unslash( $_POST['bbp_subforum_title'] ) ) : '';
	$parent_forum_id    = isset( $_POST['parent_forum_id'] ) ? absint( $_POST['parent_forum_id'] ) : 0;

	if ( empty( $bbp_subforum_title ) ) {
		wp_send_json_error( 'Sub-forum title cannot be empty.' );
	}

	if ( empty( $parent_forum_id ) ) {
		wp_send_json_error( 'Parent forum ID is required.' );
	}

	// Verify parent forum exists.
	$parent_forum = get_post( $parent_forum_id );
	if ( ! $parent_forum || 'forum' !== $parent_forum->post_type ) {
		wp_send_json_error( 'Invalid parent forum.' );
	}

	// Get order for the new sub-forum.
	$args = [
		'post_type'   => 'forum',
		'post_parent' => $parent_forum_id,
	];

	$query = new \WP_Query( $args );
	$total = $query->found_posts;
	$order = $total + 1;

	// Insert the sub-forum.
	$post_id = wp_insert_post(
		[
			'post_title'   => $bbp_subforum_title,
			'post_parent'  => $parent_forum_id,
			'post_content' => '',
			'post_type'    => 'forum',
			'post_status'  => 'publish',
			'post_author'  => get_current_user_id(),
			'menu_order'   => $order,
		]
	);

	if ( is_wp_error( $post_id ) ) {
		wp_send_json_error( 'Failed to create the sub-forum.' );
	}

	// Return success.
	wp_send_json_success( 'Sub-forum created successfully.' );
}
