<?php

namespace WarrantyCart\REST;

use WarrantyCart\REST\Controllers\Cart;
use WarrantyCart\REST\Controllers\Claims;
use WarrantyCart\REST\Controllers\Gateways;
use WarrantyCart\REST\Controllers\Orders;
use WarrantyCart\REST\Controllers\Renewals;
use WarrantyCart\REST\Controllers\Reports;
use WarrantyCart\REST\Controllers\Serials;
use WarrantyCart\REST\Controllers\Settings;
use WarrantyCart\REST\Controllers\Plugins;
use WarrantyCart\REST\Controllers\Setup;
use WarrantyCart\REST\Controllers\Tools;
use WarrantyCart\REST\Controllers\Warranties;

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

/**
 * The plugin's REST surface.
 *
 * Holds every controller and hands each one `rest_api_init` to register itself
 * on.
 */
class Routes {

	/**
	 * 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 Cart(),
			new Claims(),
			new Renewals(),
			new Warranties(),
			new Serials(),
			new Orders(),
			new Reports(),
			new Gateways(),
			new Tools(),
			new Settings(),
			new Setup(),
			new Plugins(),
		);

		$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();
		}
	}
}
