<?php
/**
 * Cart recovery email configuration.
 *
 * @package EDD\Pro\CartRecovery\Config\Emails
 * @copyright Copyright (c) 2026, Sandhills Development, LLC
 * @license https://opensource.org/licenses/GPL-2.0 GNU Public License
 * @since 3.6.5
 */

namespace EDD\Pro\CartRecovery\Config;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

/**
 * Cart recovery email configuration.
 */
final class Emails {

	/**
	 * Maximum items to display in recovery emails.
	 *
	 * @since 3.6.5
	 * @var int
	 */
	const ITEMS_DISPLAY_LIMIT = 10;

	/**
	 * Recovery token lifetime in seconds (24 hours).
	 *
	 * @since 3.6.5
	 * @var int
	 */
	const TOKEN_TTL = DAY_IN_SECONDS;

	/**
	 * Minimum recovery token lifetime in seconds (1 hour).
	 *
	 * @since 3.6.5
	 * @var int
	 */
	const TOKEN_MIN_TTL = HOUR_IN_SECONDS;

	/**
	 * Gets the configured sender name with a fallback to the store name.
	 *
	 * @since 3.6.5
	 * @return string Sender name.
	 */
	public static function get_sender_name(): string {
		if ( 'custom' === edd_get_option( 'acr_sender', '' ) ) {
			$name = trim( (string) edd_get_option( 'acr_sender_name', '' ) );
			if ( ! empty( $name ) ) {
				return $name;
			}
		}

		$name = trim( edd_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ) );
		if ( ! empty( $name ) ) {
			return $name;
		}

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

	/**
	 * Gets the configured sender email with a fallback to the store email.
	 *
	 * @since 3.6.5
	 * @return string Sender email.
	 */
	public static function get_sender_email(): string {
		if ( 'custom' === edd_get_option( 'acr_sender', '' ) ) {
			$email = trim( (string) edd_get_option( 'acr_sender_email', edd_get_option( 'from_email' ) ) );
			if ( ! empty( $email ) && is_email( $email ) ) {
				return $email;
			}
		}

		return trim( get_bloginfo( 'admin_email' ) );
	}

	/**
	 * Gets email token TTL.
	 *
	 * @since 3.6.5
	 * @return int TTL in seconds.
	 */
	public static function get_token_ttl(): int {
		return self::TOKEN_TTL;
	}

	/**
	 * Gets minimum email token TTL.
	 *
	 * @since 3.6.5
	 * @return int Minimum TTL in seconds.
	 */
	public static function get_token_min_ttl(): int {
		return self::TOKEN_MIN_TTL;
	}

	/**
	 * Gets maximum items to display in recovery emails.
	 *
	 * @since 3.6.5
	 * @return int Maximum items to show.
	 */
	public static function get_items_display_limit(): int {
		return self::ITEMS_DISPLAY_LIMIT;
	}

	/**
	 * Checks if the immediate recovery email is enabled.
	 *
	 * @since 3.6.5
	 * @return bool Whether the immediate recovery email is enabled.
	 */
	public static function is_immediate_recovery_enabled(): bool {
		$email = edd_get_email( 'edd-pro-acr-ir' );

		return $email && $email->is_enabled();
	}

	/**
	 * Checks if the value reinforcement email is enabled.
	 *
	 * @since 3.6.5
	 * @return bool Whether the value reinforcement email is enabled.
	 */
	public static function is_value_reinforcement_enabled(): bool {
		$email = edd_get_email( 'edd-pro-acr-vr' );

		return $email && $email->is_enabled();
	}

	/**
	 * Checks if the final attempt email is enabled.
	 *
	 * @since 3.6.5
	 * @return bool Whether the final attempt email is enabled.
	 */
	public static function is_final_attempt_enabled(): bool {
		$email = edd_get_email( 'edd-pro-acr-fa' );

		return $email && $email->is_enabled();
	}

	/**
	 * Gets the maximum recovery attempts.
	 *
	 * @since 3.6.5
	 * @return int Maximum recovery attempts.
	 */
	public static function get_max_recovery_attempts(): int {
		return (int) edd_get_emails(
			array(
				'count'   => true,
				'context' => 'abandoned_cart',
				'status'  => 1,
			)
		);
	}
}
