<?php
/**
 * Abandoned Cart Recovery Manager.
 *
 * Coordinates the loading of ACR event subscribers and components based on feature enablement.
 *
 * @package EDD\Pro\CartRecovery
 * @copyright Copyright (c) 2026, Sandhills Development, LLC
 * @license https://opensource.org/licenses/GPL-3.0 GNU General Public License v3.0
 * @since 3.6.5
 */

namespace EDD\Pro\CartRecovery;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

use EDD\EventManagement\MiniManager;

/**
 * Coordinates Abandoned Cart Recovery initialization.
 *
 * Loads ACR event subscribers when the feature is enabled.
 * The MiniManager base class handles automatic registration of event classes
 * on the plugins_loaded hook.
 *
 * @since 3.6.5
 */
final class Manager extends MiniManager {

	/**
	 * Get the event classes.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	protected function get_event_classes(): array {
		$classes = array(
			new Cron\Loader(),
		);
		// Always include the cron loader so we can unschedule all cron jobs if the feature is disabled.
		if ( ! edd_get_option( 'acr_enabled', false ) ) {
			return $classes;
		}

		$classes[] = new Database\Components();
		$classes[] = new Emails\Manager();
		$classes[] = new Emails\Tags();
		$classes[] = new REST\Manager();
		$classes[] = new Tracking\EmailTracker();

		// Only load CLI commands when WP-CLI is running.
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
			$classes[] = new CLI\CartRecovery();
		}

		// Initialize  cart recovery components with dependency injection.
		return array_merge( $classes, $this->get_acr_subscribers() );
	}

	/**
	 * Gets refactored ACR event subscribers with proper dependency injection.
	 *
	 * @since 3.6.5
	 * @return array Array of subscriber instances.
	 */
	private function get_acr_subscribers(): array {
		$snapshot_builder = new Snapshots\SnapshotBuilder();
		$snapshot_persist = new Snapshots\SnapshotPersistence( $snapshot_builder );
		$event_logger     = new Journey\EventLogger();
		$recovery_handler = new Handlers\Recovery();

		return array(
			new Events\CartEventSubscriber( $snapshot_persist, $event_logger ),
			new Events\CheckoutEventSubscriber( $snapshot_persist, $event_logger ),
			new Events\FrontendEventSubscriber( $recovery_handler ),
		);
	}
}
