<?php
/**
 * Action Registry Class
 *
 * Central registry for all workflow actions.
 *
 * @package Sequensy
 * @since 2.0.0
 */

declare(strict_types=1);

namespace Sequensy\Actions;

use Sequensy\Actions\Contracts\ActionHandler;
use Sequensy\Registry\AbstractRegistry;

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

/**
 * Class ActionRegistry
 *
 * Singleton registry for managing action handlers.
 * Extends AbstractRegistry for common registry functionality.
 */
class ActionRegistry extends AbstractRegistry {

	/**
	 * Singleton instance.
	 *
	 * @var ActionRegistry|null
	 */
	private static ?ActionRegistry $instance = null;

	/**
	 * Get singleton instance.
	 *
	 * @return ActionRegistry
	 */
	public static function get_instance(): ActionRegistry {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Get the WordPress action hook name for registration.
	 *
	 * @return string Hook name.
	 */
	protected function get_registration_hook(): string {
		return 'sequensy_register_actions';
	}

	/**
	 * Get the transient key for metadata caching.
	 *
	 * @return string Transient key.
	 */
	protected function get_transient_key(): string {
		return 'sequensy_action_metadata';
	}

	/**
	 * Get an action handler by type.
	 *
	 * Alias for get() with ActionHandler return type.
	 *
	 * @param string $type Action type identifier.
	 * @return ActionHandler|null Handler instance or null if not found.
	 */
	public function get_action( string $type ): ?ActionHandler {
		$handler = $this->get( $type );
		return $handler instanceof ActionHandler ? $handler : null;
	}

	/**
	 * Get all registered actions.
	 *
	 * Alias for get_all().
	 *
	 * @return array<string, ActionHandler> Array of action handlers keyed by type.
	 */
	public function get_all_actions(): array {
		return $this->get_all();
	}

	/**
	 * Check if an action type is registered.
	 *
	 * Alias for has().
	 *
	 * @param string $type Action type identifier.
	 * @return bool True if registered, false otherwise.
	 */
	public function has_action( string $type ): bool {
		return $this->has( $type );
	}
}
