<?php
/**
 * Mailer profile resolution helpers.
 *
 * @package MailDoctor
 */

namespace MailDoctor;

trait Mailer_Profile_Trait {

	/**
	 * Determine if SMTP overrides should execute for current message.
	 *
	 * @param array<string, mixed> $settings Plugin settings.
	 * @return bool
	 */
	protected static function determine_override_scope( array $settings ): bool {
		if ( self::$force_override ) {
			return true;
		}

		if ( ! empty( $settings['compatibility_mode'] ) ) {
			if ( self::$current_event && strpos( self::$current_event, 'woo' ) === 0 ) {
				return true;
			}

			return false;
		}

		if ( 'all' === ( $settings['override_scope'] ?? 'all' ) ) {
			return true;
		}

		if ( self::$current_event && strpos( self::$current_event, 'woo' ) === 0 ) {
			return true;
		}

		return false;
	}

	/**
	 * Resolve active SMTP profile for the current mail invocation.
	 *
	 * @return void
	 */
	protected static function prepare_profile(): void {
		if ( ! empty( self::$active_profile ) ) {
			return;
		}

		$profile_key = null;

		if ( isset( self::$retry_context['smtp_profile'] ) ) {
			$profile_key = self::$retry_context['smtp_profile'];
		} else {
			$context = array(
				'email_id' => self::$current_email_id,
				'roles'    => Settings::current_user_roles(),
				'site_id'  => is_multisite() ? get_current_blog_id() : 0,
			);

			$profile_key = Settings::resolve_profile_for_email( self::$current_email_id, $context );
		}

		self::set_active_profile( $profile_key );
	}

	/**
	 * Persist the active SMTP profile credentials for the current send.
	 *
	 * @param string|null $profile_slug Profile identifier.
	 * @return void
	 */
	protected static function set_active_profile( ?string $profile_slug ): void {
		$key = $profile_slug;

		if ( null === $key || '' === $key ) {
			$key = Settings::get_active_profile();
		}

		if ( is_numeric( $key ) ) {
			$key = (string) (int) $key;
		} else {
			$key = sanitize_text_field( (string) $key );
			if ( '' === $key ) {
				$key = 'default';
			}
		}

		self::$active_profile     = Settings::smtp_credentials( $key );
		self::$active_profile_key = self::$active_profile['slug'] ?? $key;
	}

	/**
	 * Resolve From email with fallbacks.
	 *
	 * @return string
	 */
	protected static function resolve_from_email(): string {
		if ( ! empty( self::$active_profile['from_email'] ) && is_email( self::$active_profile['from_email'] ) ) {
			return self::$active_profile['from_email'];
		}

		$settings   = Settings::all();
		$from_email = $settings['from_email'] ?? '';

		if ( $from_email && is_email( $from_email ) ) {
			return $from_email;
		}

		$admin_email = get_option( 'admin_email' );
		if ( $admin_email && is_email( $admin_email ) ) {
			return $admin_email;
		}

		$domain = wp_parse_url( home_url(), PHP_URL_HOST );
		if ( $domain ) {
			if ( strpos( $domain, '.' ) === false ) {
				$domain = 'example.com';
			}

			return 'no-reply@' . $domain;
		}

		return 'no-reply@example.com';
	}

	/**
	 * Resolve From name with sensible fallback.
	 *
	 * @return string
	 */
	protected static function resolve_from_name(): string {
		if ( ! empty( self::$active_profile['from_name'] ) ) {
			return self::$active_profile['from_name'];
		}

		$settings  = Settings::all();
		$from_name = $settings['from_name'] ?? '';

		if ( $from_name ) {
			return $from_name;
		}

		return wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
	}
}
