<?php
/**
 * Tracking Module
 *
 * @package AmbassadorHub\Tracking
 */

namespace AmbassadorHub\Tracking;

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

use AmbassadorHub\Tracking\DataLayerQueue;
use AmbassadorHub\Tracking\Providers\DataLayerProvider;

/**
 * Bootstraps the ambassadorhub tracking system.
 *
 * Hooks into all ambh_* domain events, builds TrackingEvent instances via
 * EventFactory, and dispatches them to registered providers.
 *
 * To add a custom provider, use the ambh_tracking_providers filter:
 *
 *   add_filter( 'ambh_tracking_providers', function( array $providers ): array {
 *       $providers[] = new MyProvider();
 *       return $providers;
 *   } );
 */
final class TrackingModule {

	/**
	 * Initialize the Tracking module.
	 *
	 * @return void
	 */
	public static function init(): void {
		// Register dataLayer provider (zero settings — consumed by GTM / gtag on the site).
		add_filter( 'ambh_tracking_providers', array( DataLayerProvider::class, 'register' ) );
		DataLayerQueue::init();

		// Donation-level events (donation_created/completed/failed) ship in the
		// AmbassadorHub Premium add-on, which hooks ambh_donation_created and
		// ambh_donation_updated directly and reuses EventFactory/TrackingDispatcher.

		// Campaigns.
		add_action( 'ambh_campaign_started', array( self::class, 'on_campaign_started' ), 10, 1 );
		add_action( 'ambh_campaign_ended', array( self::class, 'on_campaign_ended' ), 10, 1 );

		// Fundraisers.
		add_action( 'ambh_fundraiser_published', array( self::class, 'on_fundraiser_published' ), 10, 1 );

		// Accounts.
		add_action( 'ambh_ambassador_role_added', array( self::class, 'on_ambassador_registered' ), 10, 1 );

		/**
		 * Fires after the Tracking module is initialized.
		 *
		 * Use this hook to register additional tracking providers or overwrite
		 * the built-in GA4 provider registration.
		 *
		 * @since 0.4.0
		 */
		do_action( 'ambh_tracking_module_init' );
	}

	// =========================================================================
	// Hook handlers
	// =========================================================================

	/**
	 * Handle ambh_campaign_started.
	 *
	 * @param int $campaign_id Campaign post ID.
	 *
	 * @return void
	 */
	public static function on_campaign_started( int $campaign_id ): void {
		TrackingDispatcher::dispatch(
			EventFactory::from_campaign_started( $campaign_id )
		);
	}

	/**
	 * Handle ambh_campaign_ended.
	 *
	 * @param int $campaign_id Campaign post ID.
	 *
	 * @return void
	 */
	public static function on_campaign_ended( int $campaign_id ): void {
		TrackingDispatcher::dispatch(
			EventFactory::from_campaign_ended( $campaign_id )
		);
	}

	/**
	 * Handle ambh_fundraiser_published.
	 *
	 * @param int $fundraiser_id Fundraiser post ID.
	 *
	 * @return void
	 */
	public static function on_fundraiser_published( int $fundraiser_id ): void {
		TrackingDispatcher::dispatch(
			EventFactory::from_fundraiser_published( $fundraiser_id )
		);
	}

	/**
	 * Handle ambh_ambassador_role_added.
	 *
	 * @param int $user_id WordPress user ID.
	 *
	 * @return void
	 */
	public static function on_ambassador_registered( int $user_id ): void {
		TrackingDispatcher::dispatch(
			EventFactory::from_ambassador_registered( $user_id )
		);
	}
}
