<?php

namespace WarrantyCart\REST;

use WarrantyCart\REST\Controllers\OrderController;
use WarrantyCart\REST\Controllers\ReportController;
use WarrantyCart\REST\Controllers\SettingsController;
use WarrantyCart\REST\Controllers\ToolController;
use WarrantyCart\REST\Controllers\WarrantyController;

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

/**
 * REST API Manager
 *
 * Manages all REST API controllers for the plugin.
 */
class Manager {

	/**
	 * Array of REST controllers
	 *
	 * @var array
	 */
	private array $controllers = array();

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->register_hooks();
	}

	/**
	 * Initialize REST controllers
	 *
	 * Sets up all REST API controller instances.
	 */
	private function init_controllers(): void {
		$this->controllers = array(
			new WarrantyController(),
			new OrderController(),
			new ReportController(),
			new ToolController(),
			new SettingsController(),
		);

		$this->controllers = apply_filters( 'warranty_cart_rest_controllers', $this->controllers );
	}

	/**
	 * Register hooks
	 *
	 * Registers WordPress hooks for REST API initialization.
	 */
	private function register_hooks(): void {
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
	}

	/**
	 * Register all REST routes
	 *
	 * Calls register_routes on all controller instances.
	 */
	public function register_routes(): void {
		$this->init_controllers();

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