<?php
/**
 * Core loader class.
 *
 * @link          https://aleksandrsamokhin.com
 * @since         1.0.0
 *
 * @author        Aleksandr Samokhin
 * @package       Redicl\Core
 *
 * @copyright (c) 2025, Aleksandr Samokhin
 */

namespace Redicl\Core;

use Redicl\CLI\Fix_Redirects_CLI_Command;

// Abort if called directly.
defined( 'WPINC' ) || die;

/**
 * Loader class.
 */
class Loader {

	/**
	 * Holds the class instance.
	 *
	 * @var Loader $instance
	 */
	private static $instance = null;

	/**
	 * Return an instance of the class
	 *
	 * @return Loader class instance.
	 * @since 1.0.0
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Class constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$this->init();
	}

	/**
	 * Initialize the plugin.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function init() {
		// Initialize admin pages.
		$this->init_admin_pages();

		// Initialize REST API.
		$this->init_rest_api();

		// Initialize WP CLI commands.
		$this->init_wp_cli();
	}

	/**
	 * Initialize admin pages.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function init_admin_pages() {
		$redirects_page = new \Redicl\Admin\Redirects_Page();
		$redirects_page->init();
	}

	/**
	 * Initialize REST API.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function init_rest_api() {
		add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
	}

	/**
	 * Register REST API routes.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register_rest_routes() {
		$controller = new \Redicl\REST_API\Redirects_Controller();
		$controller->register_routes();
	}

	/**
	 * Initialize WP CLI commands.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function init_wp_cli() {
		if ( defined( 'WP_CLI' ) && \WP_CLI ) {
			$cli_command = new Fix_Redirects_CLI_Command();
			\WP_CLI::add_command( 'redirects', $cli_command );
		}
	}
}
