<?php
/**
 * Mailer context helpers.
 *
 * @package MailDoctor
 */

namespace MailDoctor;

trait Mailer_Context_Trait {

	/**
	 * Store current event context.
	 *
	 * @param string $event Event identifier.
	 * @return void
	 */
	public static function set_event_context( string $event ): void {
		self::$current_event = sanitize_title_with_dashes( $event );
	}

	/**
	 * Clear the event context.
	 *
	 * @return void
	 */
	public static function clear_event_context(): void {
		self::$current_event    = null;
		self::$current_order_id = null;
	}

	/**
	 * Set current order context for improved logging.
	 *
	 * @param int $order_id Order identifier.
	 * @return void
	 */
	public static function set_order_context( int $order_id ): void {
		self::$current_order_id = $order_id;
	}

	/**
	 * Store current Woo email class.
	 *
	 * @param string $email_class Class name.
	 * @return void
	 */
	public static function set_email_class( string $email_class ): void {
		self::$current_email_class = $email_class;
	}

	/**
	 * Store current Woo email identifier.
	 *
	 * @param string $email_id Woo email identifier.
	 * @return void
	 */
	public static function set_email_id( string $email_id ): void {
		self::$current_email_id = sanitize_key( $email_id );
	}

	/**
	 * Clear stored email class.
	 *
	 * @return void
	 */
	public static function clear_email_class(): void {
		self::$current_email_class = null;
	}

	/**
	 * Clear stored Woo email identifier.
	 *
	 * @return void
	 */
	public static function clear_email_id(): void {
		self::$current_email_id = null;
	}

	/**
	 * Provide retry context for next wp_mail call.
	 *
	 * @param array<string, mixed> $context Retry metadata.
	 * @return void
	 */
	public static function set_retry_context( array $context ): void {
		self::debug_log( 'set_retry_context', $context );
		self::$retry_context  = $context;
		self::$force_override = true;
		if ( isset( $context['correlation_id'] ) && $context['correlation_id'] ) {
			self::$correlation_id = self::sanitize_correlation_value( (string) $context['correlation_id'] );
		} elseif ( null === self::$correlation_id ) {
			self::$correlation_id = self::ensure_correlation_id();
		}
		if ( isset( $context['smtp_profile'] ) ) {
			self::set_active_profile( $context['smtp_profile'] );
		}
	}

	/**
	 * Force SMTP override for the next mail invocation.
	 *
	 * @return void
	 */
	public static function force_next_override(): void {
		self::$force_override = true;
	}

	/**
	 * Reset runtime context after a send completes.
	 *
	 * @return void
	 */
	public static function cleanup_context(): void {
		self::debug_log( 'cleanup_context' );
		self::$force_override        = false;
		self::$retry_context         = array();
		self::$active_log_id         = null;
		self::$should_override       = true;
		self::$current_order_id      = null;
		self::$current_email_class   = null;
		self::$current_email_id      = null;
		self::$idempotency_key       = null;
		self::$idempotency_bucket    = null;
		self::$idempotency_duplicate = false;
		self::$active_profile_key    = null;
		self::$active_profile        = array();
		self::$attachment_meta       = array();
		self::$attachment_total      = 0;
		self::$bypass_plugin         = false;
		self::$thread_headers        = array(
			'message_id'  => null,
			'in_reply_to' => null,
		);
		self::$correlation_id        = null;
	}
}
