<?php
/**
 * REST API Service Provider
 *
 * Registers REST API routes for workflows and integrations.
 *
 * @package Sequensy
 * @since 2.0.0
 */

declare(strict_types=1);

namespace Sequensy\Providers;

use Sequensy\Container;
use Sequensy\Rest\WorkflowController;
use Sequensy\Rest\WorkflowStateController;
use Sequensy\Rest\VersionController;
use Sequensy\Rest\MetadataController;
use Sequensy\Rest\ActivityController;
use Sequensy\Rest\IntegrationsController;
use Sequensy\Rest\PackageController;
use Sequensy\Rest\SettingsController;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Class RestApiServiceProvider
 *
 * Provides REST API route registration using factory pattern for controllers.
 */
class RestApiServiceProvider implements ServiceProvider {

	/**
	 * Service container instance.
	 *
	 * @var Container|null
	 */
	private ?Container $container = null;

	/**
	 * Register REST API services in the container.
	 *
	 * @param Container $container Service container instance.
	 * @return void
	 */
	public function register( Container $container ): void {
		// Register controller factories for explicit DI.
		$container->register(
			'workflow_controller',
			function ( $c ) {
				return WorkflowController::create( $c );
			}
		);

		$container->register(
			'workflow_state_controller',
			function ( $c ) {
				return WorkflowStateController::create( $c );
			}
		);

		$container->register(
			'version_controller',
			function ( $c ) {
				return VersionController::create( $c );
			}
		);

		$container->register(
			'metadata_controller',
			function ( $c ) {
				return MetadataController::create( $c );
			}
		);

		$container->register(
			'activity_controller',
			function ( $c ) {
				return ActivityController::create( $c );
			}
		);

		$container->register(
			'integrations_controller',
			function () {
				return new IntegrationsController();
			}
		);

		$container->register(
			'settings_controller',
			function () {
				return new SettingsController();
			}
		);

		$container->register(
			'free_package_controller',
			function ( $c ) {
				return PackageController::create( $c );
			}
		);
	}

	/**
	 * Boot the REST API service provider.
	 *
	 * Registers REST API routes.
	 *
	 * @param Container $container Service container instance.
	 * @return void
	 */
	public function boot( Container $container ): void {
		$this->container = $container;
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
	}

	/**
	 * Register REST API routes.
	 */
	public function register_routes(): void {
		if ( ! $this->container ) {
			return;
		}

		// Workflow CRUD operations.
		$workflow_controller = $this->container->get( 'workflow_controller' );
		$workflow_controller->register_routes();

		// Workflow state management (activate/deactivate).
		$workflow_state_controller = $this->container->get( 'workflow_state_controller' );
		$workflow_state_controller->register_routes();

		// Workflow version management.
		$version_controller = $this->container->get( 'version_controller' );
		$version_controller->register_routes();

		// Metadata (triggers, actions, system info).
		$metadata_controller = $this->container->get( 'metadata_controller' );
		$metadata_controller->register_routes();

		// Activity logs.
		$activity_controller = $this->container->get( 'activity_controller' );
		$activity_controller->register_routes();

		// Integrations management.
		$integrations_controller = $this->container->get( 'integrations_controller' );
		$integrations_controller->register_routes();

		// Plugin settings.
		$settings_controller = $this->container->get( 'settings_controller' );
		$settings_controller->register_routes();

		// Package management (list, activate, deactivate).
		$package_controller = $this->container->get( 'free_package_controller' );
		$package_controller->register_routes();
	}
}
