<?php
/**
 * Registry Service Provider
 *
 * Registers trigger and action registries and fires registration hooks.
 *
 * @package Sequensy
 * @since 2.0.0
 */

declare(strict_types=1);

namespace Sequensy\Providers;

use Sequensy\Container;
use Sequensy\Triggers\TriggerRegistry;
use Sequensy\Actions\ActionRegistry;
use Sequensy\Integrations\IntegrationStorage;

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

/**
 * Class RegistryServiceProvider
 *
 * Provides trigger and action registries, and registers default handlers.
 */
class RegistryServiceProvider implements ServiceProvider {

	/**
	 * Register registry services in the container.
	 *
	 * @param Container $container Service container instance.
	 * @return void
	 */
	public function register( Container $container ): void {
		$container->register(
			'trigger_registry',
			function () {
				return TriggerRegistry::get_instance();
			}
		);

		$container->register(
			'action_registry',
			function () {
				return ActionRegistry::get_instance();
			}
		);

		// Integration storage.
		$container->register(
			'integration_storage',
			function () {
				return new IntegrationStorage();
			}
		);
	}

	/**
	 * Boot the registry service provider.
	 *
	 * Initializes registries and fires registration hooks for triggers and actions.
	 *
	 * @param Container $container Service container instance.
	 * @return void
	 */
	public function boot( Container $container ): void {
		// Register default triggers and actions via hooks.
		add_action( 'sequensy_register_triggers', array( $this, 'register_default_triggers' ) );
		add_action( 'sequensy_register_actions', array( $this, 'register_default_actions' ) );

		// Initialize registries early to ensure their init hooks are registered.
		// This ensures the registration hooks fire during the init action.
		$container->get( 'trigger_registry' );
		$container->get( 'action_registry' );
	}

	/**
	 * Register default triggers.
	 *
	 * All bundled triggers have been moved to marketplace packages.
	 * This method now only fires the discovery hook for extensions.
	 *
	 * @param TriggerRegistry $registry Trigger registry instance.
	 */
	public function register_default_triggers( TriggerRegistry $registry ): void {
		/**
		 * Fires after default triggers are registered.
		 *
		 * Allows plugins to register additional triggers after core registration.
		 *
		 * @since 2.0.0
		 *
		 * @param TriggerRegistry $registry Trigger registry instance.
		 */
		do_action( 'sequensy_triggers_discovered', $registry );
	}

	/**
	 * Register default actions.
	 *
	 * All bundled actions have been moved to marketplace packages.
	 * This method now only fires the discovery hook for extensions.
	 *
	 * @param ActionRegistry $registry Action registry instance.
	 */
	public function register_default_actions( ActionRegistry $registry ): void {
		/**
		 * Fires after default actions are registered.
		 *
		 * Allows plugins to register additional actions after core registration.
		 *
		 * @since 2.0.0
		 *
		 * @param ActionRegistry $registry Action registry instance.
		 */
		do_action( 'sequensy_actions_discovered', $registry );
	}
}
