<?php
/**
 * Accounts Module Automations
 *
 * Registers automation triggers, conditions, and event handlers for account-related workflows.
 *
 * @package AmbassadorHub\Accounts
 */

namespace AmbassadorHub\Campaigns;

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

use AmbassadorHub\Automations\AbstractAutomations;
use AmbassadorHub\Automations\ScheduleEngine;

/**
 * Consolidates automation registration and event handling for the Accounts module.
 *
 * This class registers:
 * - Automation triggers (account_created, account_updated, account_deleted)
 * - Automation conditions (donations_total_eur, donations_count)
 * - Metric value providers for condition evaluation
 * - Event handlers for account lifecycle events
 */
final class Automations extends AbstractAutomations {


	/**
	 * Register module-specific event handlers.
	 *
	 * Called by AbstractAutomations::init() during initialization.
	 *
	 * @return void
	 */
	protected static function register_event_handlers(): void {

		// Register event emitters for campaign lifecycle
		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 );

		// Handle webhook payload for campaign-related actions
		add_filter( 'ambh_get_webhook_action_payload_campaign', array( self::class, 'get_campaign_webhook_payload' ), 10, 3 );
	}

	public static function register_trigger_groups( array $groups ): array {
		$groups['campaigns'] = array(
			'label' => esc_html__( 'Campaign Events', 'ambassadorhub' ),
			'icon'  => 'dashicons-megaphone',
		);

		return $groups;
	}

	public static function register_triggers( array $triggers ): array {
		$triggers['campaign_created'] = array(
			'label'       => esc_html__( 'Campaign Created', 'ambassadorhub' ),
			'description' => esc_html__( 'Triggered when a new campaign is created.', 'ambassadorhub' ),
			'group'       => 'campaigns',
			'entity_type' => 'campaign',
			'event'       => 'created',
			'icon'        => 'dashicons-megaphone',
		);

		$triggers['campaign_updated'] = array(
			'label'       => esc_html__( 'Campaign Updated', 'ambassadorhub' ),
			'description' => esc_html__( 'Triggered when a campaign is updated.', 'ambassadorhub' ),
			'group'       => 'campaigns',
			'entity_type' => 'campaign',
			'event'       => 'updated',
			'icon'        => 'dashicons-update',
		);

		$triggers['campaign_started'] = array(
			'label'       => esc_html__( 'Campaign Started', 'ambassadorhub' ),
			'description' => esc_html__( 'Triggered when a campaign is started (published).', 'ambassadorhub' ),
			'group'       => 'campaigns',
			'entity_type' => 'campaign',
			'event'       => 'started',
			'icon'        => 'dashicons-controls-play',
		);

		$triggers['campaign_ended'] = array(
			'label'       => esc_html__( 'Campaign Ended', 'ambassadorhub' ),
			'description' => esc_html__( 'Triggered when a campaign is marked as ended.', 'ambassadorhub' ),
			'group'       => 'campaigns',
			'entity_type' => 'campaign',
			'event'       => 'ended',
			'icon'        => 'dashicons-no',
		);

		return $triggers;
	}

	public static function on_campaign_started( int $campaign_id ): void {

		ScheduleEngine::schedule_rules_for_trigger(
			'campaign_started',
			$campaign_id,
			array(
				'campaign_id' => $campaign_id,
				'source'      => 'campaign_started',
			)
		);
	}

	public static function on_campaign_ended( int $campaign_id ): void {

		ScheduleEngine::schedule_rules_for_trigger(
			'campaign_ended',
			$campaign_id,
			array(
				'campaign_id' => $campaign_id,
				'source'      => 'campaign_ended',
			)
		);
	}

	public static function get_campaign_webhook_payload( array $payload, int $target_id, $action_id ): array {

		if ( ! $target_id ) {
			return $payload;
		}

		$campaign = get_post( $target_id );
		if ( ! $campaign ) {
			return $payload;
		}

		$payload['campaign'] = array(
			'ID'     => $campaign->ID,
			'title'  => $campaign->post_title,
			'status' => $campaign->post_status,
		);

		return $payload;
	}
}
