<?php
/**
 * Dashboard REST controller registry.
 *
 * Hooks `rest_api_init` once and instantiates every Free dashboard
 * controller in one place — the single point a reader visits to know
 * what routes ship under `/blocksify/v1/*`. Pro plugins extend by
 * hooking `blocksify_dashboard_register_rest_routes` (fired after every
 * Free controller registers).
 *
 * @package Blocksify
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * REST registry. Instance-less; methods are static.
 */
class Blocksify_Dashboard_REST {

	/**
	 * Wire the registry to `rest_api_init`. Called once from blocksify.php.
	 */
	public static function boot() {
		add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
	}

	/**
	 * Instantiate + register every Free dashboard controller. Order isn't
	 * load-bearing (REST routes don't shadow each other) but keep the list
	 * grouped by concept for readability.
	 */
	public static function register_routes() {
		( new Blocksify_Dashboard_Settings_Controller() )->register_routes();
		( new Blocksify_Dashboard_Settings_Schema_Controller() )->register_routes();
		( new Blocksify_Dashboard_Changelog_Controller() )->register_routes();
		( new Blocksify_Dashboard_Clear_Cache_Controller() )->register_routes();
		( new Blocksify_Dashboard_Rollback_Controller() )->register_routes();
		( new Blocksify_Dashboard_Onboarding_Controller() )->register_routes();

		/**
		 * Fires after Free dashboard REST routes register. Pro registers
		 * its own controllers here under the dashboard's permission
		 * pattern. SPEC §11.2.
		 *
		 * @param WP_REST_Server $server Active REST server.
		 */
		do_action( 'blocksify_dashboard_register_rest_routes', rest_get_server() );
	}

	/**
	 * Return the list of REST namespaces owned by the dashboard surface.
	 * Free declares `'blocksify/v1'`; Pro extends to include
	 * `'blocksify-pro/v1'` via the filter. Documentation / inspection
	 * helper — not used to gate auth.
	 *
	 * @return string[]
	 */
	public static function namespaces() {
		/**
		 * REST namespaces under the dashboard auth surface. SPEC §5.3 / §11.2.
		 *
		 * @param string[] $namespaces Default `['blocksify/v1']`.
		 */
		$namespaces = apply_filters( 'blocksify_dashboard_rest_namespaces', array( 'blocksify/v1' ) );
		return is_array( $namespaces ) ? array_values( array_unique( array_filter( array_map( 'strval', $namespaces ) ) ) ) : array( 'blocksify/v1' );
	}
}
