<?php
/**
 * REST API base handler.
 *
 * Registers all REST API routes for the plugin by initializing
 * individual endpoint controllers.
 *
 * @package Antimanual_Builder
 * @since   1.0.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class AMBLD_API_Rest_Api
 *
 * Bootstraps all REST API controllers.
 *
 * @since 1.0.0
 */
class AMBLD_API_Rest_Api {

	/**
	 * REST namespace.
	 *
	 * @since 1.0.0
	 * @var   string
	 */
	const NAMESPACE = 'ambld/v1';

	/**
	 * Constructor. Registers the REST API initialization hook.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
	}

	/**
	 * Register all API routes.
	 *
	 * @since  1.0.0
	 * @return void
	 */
	public function register_routes() {
		// Include Chat API manually (non-standard class naming).
		require_once AMBLD_PLUGIN_DIR . 'includes/api/class-chat-api.php';
		
		// Include Builder Meta Trait for shared saving logic.
		require_once AMBLD_PLUGIN_DIR . 'includes/api/trait-builder-meta-api.php';

		// Include Dependency API for plugin activation.
		require_once AMBLD_PLUGIN_DIR . 'includes/api/class-dependency-api.php';

		// Include Shortcode API for shortcode rendering.
		require_once AMBLD_PLUGIN_DIR . 'includes/api/class-shortcode-api.php';

		// Include Conversations API for persistent chat history.
		require_once AMBLD_PLUGIN_DIR . 'includes/api/class-conversations-api.php';

		$controllers = array(
			new AMBLD_API_Pages_Api(),
			new AMBLD_API_Ai_Api(),
			new AMBLD_API_Components_Api(),
			new AMBLD_API_Templates_Api(),
			new AMBLD_API_Chat_Api(),
			new AMBLD_API_Dependency_Api(),
			new AMBLD_API_Shortcode_Api(),
			new AMBLD_API_Conversations_Api(),
		);

		foreach ( $controllers as $controller ) {
			$controller->register_routes();
		}
	}

}
