<?php

namespace WarrantyCart\Integrations;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Integration Manager
 *
 * Manages integrations with external WooCommerce payment plugins.
 *
 * @since 1.0.0
 */
class IntegrationManager {

	/**
	 * Available integrations
	 *
	 * @var array<string, AbstractIntegration>
	 */
	private array $integrations = array();

	/**
	 * Active integrations
	 *
	 * @var array<string, AbstractIntegration>
	 */
	private array $active_integrations = array();

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->init_integrations();
		$this->load_active_integrations();
	}

	/**
	 * Initialize available integrations
	 *
	 * @return void
	 */
	private function init_integrations(): void {
		$this->integrations = array(
			'paypal' => new PayPalIntegration(),
			'stripe' => new StripeIntegration(),
		);

		/**
		 * Filter available integrations before initialization
		 *
		 * @param array<string, AbstractIntegration> $integrations Available integrations.
		 */
		$this->integrations = \apply_filters( 'warranty_cart_available_integrations', $this->integrations );

		/**
		 * Action to register custom integrations
		 *
		 * @param IntegrationManager $manager Integration manager instance.
		 */
		\do_action( 'warranty_cart_register_integrations', $this );
	}

	/**
	 * Load active integrations
	 *
	 * @return void
	 */
	private function load_active_integrations(): void {
		foreach ( $this->integrations as $integration_id => $integration ) {
			if ( $integration->is_ready() ) {
				$this->active_integrations[ $integration_id ] = $integration;
				$integration->init();
			}
		}
	}

	/**
	 * Get all available integrations
	 *
	 * @return array<string, AbstractIntegration>
	 */
	public function get_all_integrations(): array {
		/**
		 * Filter all available integrations.
		 *
		 * @param array<string, AbstractIntegration> $integrations All integrations.
		 */
		return \apply_filters( 'warranty_cart_integrations_all', $this->integrations );
	}

	/**
	 * Get active integrations
	 *
	 * @return array<string, AbstractIntegration>
	 */
	public function get_active_integrations(): array {
		/**
		 * Filter active integrations.
		 *
		 * @param array<string, AbstractIntegration> $active_integrations Active integrations.
		 */
		return \apply_filters( 'warranty_cart_integrations_active', $this->active_integrations );
	}

	/**
	 * Get integration by ID
	 *
	 * @param string $integration_id Integration ID.
	 *
	 * @return AbstractIntegration|null
	 */
	public function get_integration( string $integration_id ): ?AbstractIntegration {
		return $this->integrations[ $integration_id ] ?? null;
	}

	/**
	 * Check if integration is active
	 *
	 * @param string $integration_id Integration ID.
	 *
	 * @return bool
	 */
	public function is_integration_active( string $integration_id ): bool {
		return isset( $this->active_integrations[ $integration_id ] );
	}

	/**
	 * Get integration status information
	 *
	 * @return array
	 */
	public function get_integration_status(): array {
		$status = array();

		foreach ( $this->integrations as $integration_id => $integration ) {
			$status[ $integration_id ] = array(
				'id'            => $integration->get_id(),
				'name'          => $integration->get_name(),
				'description'   => $integration->get_description(),
				'is_active'     => $integration->is_plugin_active(),
				'is_ready'      => $integration->is_ready(),
				'is_compatible' => $integration->is_plugin_compatible(),
			);
		}

		return $status;
	}

	/**
	 * Magic getter for integration access
	 *
	 * @param string $name Integration name.
	 *
	 * @return AbstractIntegration|null
	 */
	public function __get( string $name ): ?AbstractIntegration {
		return $this->active_integrations[ $name ] ?? null;
	}
}
