<?php
/**
 * Consent Module Automations
 *
 * Registers automation actions for consent events.
 *
 * @package AmbassadorHub\Consent
 */

namespace AmbassadorHub\Consent;

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

use AmbassadorHub\Automations\AbstractAutomations;
use AmbassadorHub\Automations\ScheduleEngine;
use AmbassadorHub\Donations\DonationsModule;

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 account lifecycle
		add_action( 'ambh_consent_granted', array( self::class, 'on_consent_granted' ), 10, 4 );
		add_action( 'ambh_consent_withdrawn', array( self::class, 'on_consent_withdrawn' ), 10, 4 );

		// Handle webhook payload for account-related actions
		add_filter( 'ambh_get_webhook_action_payload_consent', array( self::class, 'get_consent_webhook_payload' ), 10, 3 );
	}

	/**
	 * Register trigger groups for the Accounts module.
	 *
	 * @param array $groups Existing trigger groups.
	 *
	 * @return array Modified groups.
	 */
	public static function register_trigger_groups( array $groups ): array {
		$groups['consent'] = array(
			'label' => esc_html__( 'Consent updates', 'ambassadorhub' ),
			'icon'  => 'dashicons-lock',
		);

		return $groups;
	}

	/**
	 * Register automation actions for consent events.
	 *
	 * @param array $actions Existing actions.
	 * @return array Modified actions.
	 */
	public static function register_triggers( array $triggers ): array {
		$triggers['consent_granted']   = array(
			'label'       => __( 'Consent granted', 'ambassadorhub' ),
			'description' => __( 'Triggered when a user or donor grants consent for a specific purpose.', 'ambassadorhub' ),
			'group'       => 'consent',
			'entity_type' => 'consent',
			'event'       => 'granted',
			'icon'        => 'dashicons-yes',
		);
		$triggers['consent_withdrawn'] = array(
			'label'       => __( 'Consent withdrawn', 'ambassadorhub' ),
			'description' => __( 'Triggered when a user or donor withdraws consent for a specific purpose.', 'ambassadorhub' ),
			'group'       => 'consent',
			'entity_type' => 'consent',
			'event'       => 'withdrawn',
			'icon'        => 'dashicons-no',
		);
		return $triggers;
	}


	/**
	 * Handle consent granted event.
	 *
	 * @param int $record_id The ID of the user or donor granting consent.
	 * @param string $subject_type The type of entity granting consent (e.g. 'wp_user', 'donor').
	 * @param int $subject_id The ID of the entity granting consent.
	 * @param string $purpose_slug The slug of the consent purpose.
	 *
	 * @return void
	 */
	public static function on_consent_granted( $record_id, $subject_type, $subject_id, $purpose_slug ): void {

		ScheduleEngine::schedule_rules_for_trigger(
			'consent_granted',
			$record_id,
			array(
				'subject_type' => $subject_type,
				'subject_id'   => $subject_id,
				'purpose_slug' => $purpose_slug,
			)
		);
	}

	/**
	 * Handle consent withdrawn event.
	 *
	 * @param int $record_id The ID of the user or donor withdrawing consent.
	 * @param string $subject_type The type of entity withdrawing consent (e.g. 'wp_user', 'donor').
	 * @param int $subject_id The ID of the entity withdrawing consent.
	 * @param string $purpose_slug The slug of the consent purpose.
	 *
	 * @return void
	 */
	public static function on_consent_withdrawn( $record_id, $subject_type, $subject_id, $purpose_slug ): void {

		ScheduleEngine::schedule_rules_for_trigger(
			'consent_withdrawn',
			$record_id,
			array(
				'subject_type' => $subject_type,
				'subject_id'   => $subject_id,
				'purpose_slug' => $purpose_slug,
			)
		);
	}


	/**
	 * Provide webhook payload for consent events.
	 *
	 * @param array $payload Existing payload data.
	 * @param string $target_id The ID of the entity related to the trigger.
	 * @param int $action_id The ID of the action related to the trigger.
	 * @return array Modified payload data.
	 */
	public static function get_consent_webhook_payload( array $payload, string $target_id, int $action_id ): array {

		$record = ConsentManager::get( (int) $target_id );
		if ( ! $record ) {
			return $payload;
		}

		$payload['source_record'] = array(
			'id'           => $record->id,
			'subject_type' => $record->subject_type,
			'subject_id'   => $record->subject_id,
			'purpose_slug' => $record->purpose_slug,
			'channel'      => $record->channel,
			'scope_type'   => $record->scope_type,
			'scope_id'     => $record->scope_id,
			'granted'      => (bool) $record->granted,
			'consent_text' => $record->consent_text,
			'source'       => $record->source,
			'consented_at' => $record->consented_at,
			'withdrawn_at' => $record->withdrawn_at,
			'withdrawn_by' => $record->withdrawn_by,
		);

		$payload['consent'] = array(
			'slug'           => $record->purpose_slug,
			'channel'        => $record->channel,
			'scope_type'     => $record->scope_type,
			'scope_id'       => $record->scope_id,
			'consent_status' => $record->granted ? 'granted' : 'withdrawn',
			'email'          => ConsentModule::get_subject_email( $record->subject_type, $record->subject_id ),

		);

		return $payload;
	}
}
