<?php
/**
 * Block Registration and Orchestration.
 *
 * This class handles Gutenberg block registration, editor assets,
 * and block category setup. Individual block render callbacks are
 * delegated to dedicated classes inside the Render/ directory.
 *
 * @package BBP_Core
 */

namespace admin\Blocks;

// Load render classes.
require_once __DIR__ . '/Render/forum-posts.php';
require_once __DIR__ . '/Render/forum-tab.php';
require_once __DIR__ . '/Render/forums.php';
require_once __DIR__ . '/Render/search.php';
require_once __DIR__ . '/Render/single-forum.php';
require_once __DIR__ . '/Render/forum-ajax.php';
require_once __DIR__ . '/Render/forum-overview.php';


use admin\Blocks\Render\Forum_Posts;
use admin\Blocks\Render\Forum_Tab;
use admin\Blocks\Render\Forums;
use admin\Blocks\Render\Search;
use admin\Blocks\Render\Single_Forum;
use admin\Blocks\Render\Forum_Ajax;
use admin\Blocks\Render\Forum_Overview;


class Blocks_Register {

	/**
	 * Render class instances.
	 *
	 * @var object[]
	 */
	private $renderers = array();

	/**
	 * Constructor — hooks into WordPress.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'blocks_init' ) );
		add_filter( 'block_categories_all', array( $this, 'register_block_category' ), 10, 2 );
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );

		// Instantiate render classes.
		$this->renderers['forum_posts']  = new Forum_Posts();
		$this->renderers['forum_tab']    = new Forum_Tab();
		$this->renderers['forums']       = new Forums();
		$this->renderers['search']       = new Search();
		$this->renderers['single_forum'] = new Single_Forum();
		$this->renderers['forum_ajax']   = new Forum_Ajax();
		$this->renderers['forum_overview']  = new Forum_Overview();


	}

	/**
	 * Register the Forumax block category.
	 *
	 * @param array   $categories Existing categories.
	 * @param WP_Post $post       Current post.
	 * @return array Modified categories.
	 */
	public function register_block_category( $categories, $post ) {
		return array_merge(
			$categories,
			array(
				array(
					'slug'  => 'forumax',
					'title' => __( 'Forumax', 'forumax' ),
				),
			)
		);
	}

	/**
	 * Enqueue assets for the Gutenberg block editor.
	 *
	 * This ensures the AJAX URL and upsell configuration
	 * are available for block scripts.
	 */
	public function enqueue_editor_assets() {
		$is_pro_active = class_exists( 'Forumax_Pro' ) && function_exists( 'bc_fs' ) && bc_fs()->can_use_premium_code();

		wp_localize_script( 'wp-edit-post', 'bbpc_editor_config', array(
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
			'nonce'   => wp_create_nonce( 'forumax-nonce' ),
		) );

		wp_localize_script( 'wp-edit-post', 'bbpc_upsell_config', array(
			'is_pro_active'    => $is_pro_active,
			'upsell_image_url' => defined( 'FORUMAX_IMG' ) ? FORUMAX_IMG . 'upsell-forum-ajax.png' : '',
			'upgrade_url'      => admin_url( 'admin.php?page=forumax-pricing' ),
		) );
	}

	/**
	 * Register all Gutenberg blocks.
	 *
	 * Each block points to its compiled build directory and
	 * delegates rendering to the appropriate Render class.
	 */
	public function blocks_init() {

		$blocks = array(
			'forum-posts'    => 'forum_posts',
			'forum-tab'      => 'forum_tab',
			'forums'         => 'forums',
			'search'         => 'search',
			'single-forum'   => 'single_forum',
			'forum-ajax'     => 'forum_ajax',
			'forum-overview' => 'forum_overview',
		);

		foreach ( $blocks as $block_dir => $renderer_key ) {
			if ( 'forum-ajax' === $block_dir && \WP_Block_Type_Registry::get_instance()->is_registered( 'bbp-core/forum-ajax' ) ) {
				continue;
			}

			$block_path = __DIR__ . '/../../build/' . $block_dir;
			if ( file_exists( $block_path . '/block.json' ) ) {
				register_block_type( $block_path, array(
					'render_callback' => array( $this->renderers[ $renderer_key ], 'render_' . $renderer_key ),
				) );
			}
		}


	}
}
