<?php
/**
 * Integration Metadata Registry
 *
 * Static registry for integration metadata that can be queried without
 * full integration initialization. Enables always-visible action listings
 * in the workflow builder even when integrations are not connected.
 *
 * @package Sequensy
 * @since 3.0.0
 */

declare(strict_types=1);

namespace Sequensy\Integrations;

use Sequensy\Actions\ActionDefinition;

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

/**
 * Class IntegrationMetadataRegistry
 *
 * Holds static metadata for all integrations, allowing the workflow builder
 * to display all available actions without loading full integration code.
 */
class IntegrationMetadataRegistry {

	/**
	 * Registered integration metadata.
	 *
	 * @var array<string, array{
	 *     slug: string,
	 *     metadata: array,
	 *     actions: array<ActionDefinition>
	 * }>
	 */
	private static array $registry = array();

	/**
	 * Register integration metadata.
	 *
	 * @param string $slug     Integration slug identifier.
	 * @param array  $metadata Integration metadata from get_static_metadata().
	 * @return void
	 */
	public static function register( string $slug, array $metadata ): void {
		self::$registry[ $slug ] = $metadata;
	}

	/**
	 * Get all registered integration metadata.
	 *
	 * @return array<string, array> All registered integrations.
	 */
	public static function get_all(): array {
		return self::$registry;
	}

	/**
	 * Get metadata for a specific integration.
	 *
	 * @param string $slug Integration slug.
	 * @return array|null Integration metadata or null if not found.
	 */
	public static function get( string $slug ): ?array {
		return self::$registry[ $slug ] ?? null;
	}

	/**
	 * Get action definitions for a specific integration.
	 *
	 * @param string $slug Integration slug.
	 * @return array<ActionDefinition> Array of action definitions.
	 */
	public static function get_action_definitions( string $slug ): array {
		$integration = self::$registry[ $slug ] ?? null;
		if ( null === $integration ) {
			return array();
		}

		return $integration['actions'] ?? array();
	}

	/**
	 * Get all action definitions across all integrations.
	 *
	 * @return array<ActionDefinition> Flat array of all action definitions.
	 */
	public static function get_all_action_definitions(): array {
		$all_actions = array();

		foreach ( self::$registry as $integration ) {
			$actions = $integration['actions'] ?? array();
			foreach ( $actions as $action ) {
				$all_actions[] = $action;
			}
		}

		return $all_actions;
	}

	/**
	 * Check if an integration is registered.
	 *
	 * @param string $slug Integration slug.
	 * @return bool True if registered.
	 */
	public static function has( string $slug ): bool {
		return isset( self::$registry[ $slug ] );
	}

	/**
	 * Get list of all registered integration slugs.
	 *
	 * @return array<string> Integration slugs.
	 */
	public static function get_slugs(): array {
		return array_keys( self::$registry );
	}

	/**
	 * Clear all registered metadata.
	 *
	 * Primarily used for testing.
	 *
	 * @return void
	 */
	public static function clear(): void {
		self::$registry = array();
	}
}
