<?php

namespace WarrantyCart\Integrations;

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

/**
 * Abstract Integration Class
 *
 * Base class for integrating with external WooCommerce payment plugins.
 * Handles plugin detection, activation checks, and provides common integration methods.
 *
 * @since 1.0.0
 */
abstract class AbstractIntegration {

	/**
	 * Plugin slug for detection
	 *
	 * @var string
	 */
	protected string $plugin_slug;

	/**
	 * Plugin name for display
	 *
	 * @var string
	 */
	protected string $plugin_name;

	/**
	 * Minimum required version
	 *
	 * @var string
	 */
	protected string $min_version;

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

	/**
	 * Initialize integration hooks
	 *
	 * @return void
	 */
	protected function init_hooks(): void {
		add_action( 'init', array( $this, 'check_plugin_compatibility' ) );
	}

	/**
	 * Get integration ID
	 *
	 * @return string
	 */
	abstract public function get_id(): string;

	/**
	 * Get integration name
	 *
	 * @return string
	 */
	abstract public function get_name(): string;

	/**
	 * Get integration description
	 *
	 * @return string
	 */
	abstract public function get_description(): string;

	/**
	 * Check if the external plugin is active and compatible
	 *
	 * @return bool
	 */
	public function is_plugin_active(): bool {
		if ( ! function_exists( 'is_plugin_active' ) ) {
			return false;
		}

		return is_plugin_active( $this->plugin_slug );
	}

	/**
	 * Check plugin version compatibility
	 *
	 * @return bool
	 */
	public function is_plugin_compatible(): bool {
		if ( ! $this->is_plugin_active() ) {
			return false;
		}

		$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $this->plugin_slug, false, false );

		if ( empty( $plugin_data['Version'] ) ) {
			return false;
		}

		return version_compare( $plugin_data['Version'], $this->min_version, '>=' );
	}

	/**
	 * Check plugin compatibility and show admin notices if needed
	 *
	 * @return void
	 */
	public function check_plugin_compatibility(): void {
		if ( ! is_admin() ) {
			return;
		}

		if ( ! $this->is_plugin_active() ) {
			add_action( 'admin_notices', array( $this, 'plugin_not_active_notice' ) );

			return;
		}

		if ( ! $this->is_plugin_compatible() ) {
			add_action( 'admin_notices', array( $this, 'plugin_version_notice' ) );
		}
	}

	/**
	 * Display notice when plugin is not active
	 *
	 * @return void
	 */
	public function plugin_not_active_notice(): void {
		?>
		<div class="notice notice-warning is-dismissible">
			<p>
				<?php
				printf(
				/* translators: %s: Plugin name */
					esc_html__( 'Warranty Cart: %s plugin is not active. Warranty tracking may not work properly.', 'warranty-cart' ),
					esc_html( $this->plugin_name )
				);
				?>
			</p>
		</div>
		<?php
	}

	/**
	 * Display notice when plugin version is incompatible
	 *
	 * @return void
	 */
	public function plugin_version_notice(): void {
		$plugin_data     = get_plugin_data( WP_PLUGIN_DIR . '/' . $this->plugin_slug );
		$current_version = $plugin_data['Version'] ?? 'unknown';
		?>
		<div class="notice notice-warning is-dismissible">
			<p>
				<?php
				printf(
				/* translators: 1: Plugin name, 2: Current version, 3: Minimum version */
					esc_html__( 'Warranty Cart: %1$s plugin version %2$s is not compatible. Minimum required version is %3$s.', 'warranty-cart' ),
					esc_html( $this->plugin_name ),
					esc_html( $current_version ),
					esc_html( $this->min_version )
				);
				?>
			</p>
		</div>
		<?php
	}

	/**
	 * Initialize the integration
	 *
	 * Called when the integration is ready to be initialized
	 *
	 * @return void
	 */
	abstract public function init(): void;

	/**
	 * Check if integration is ready to be used
	 *
	 * @return bool
	 */
	public function is_ready(): bool {
		return $this->is_plugin_active() && $this->is_plugin_compatible();
	}
}
