<?php
/**
 * Final Attempt Abandoned Cart email template.
 *
 * @package EDD\Pro\CartRecovery\Emails\Templates
 * @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\Emails\Templates;

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

/**
 * Class FinalAttempt
 *
 * @since 3.6.5
 * @package EDD\Pro\CartRecovery\Emails\Templates
 */
class FinalAttempt extends Base {
	/**
	 * Unique identifier for this template.
	 *
	 * @since 3.6.5
	 * @var string
	 */
	protected $email_id = 'edd-pro-acr-fa';

	/**
	 * The email meta.
	 *
	 * @var array
	 */
	protected $meta = array(
		'delay'         => 72 * HOUR_IN_SECONDS,
		'unit'          => HOUR_IN_SECONDS,
		'discount_code' => '',
	);

	/**
	 * Name of the template.
	 *
	 * @since 3.6.5
	 * @return string
	 */
	public function get_name() {
		return __( 'Final Attempt', 'easy-digital-downloads' );
	}

	/**
	 * Description of the email.
	 *
	 * @since 3.6.5
	 * @return string
	 */
	public function get_description() {
		$hours = $this->get_metadata( 'delay' );

		/* translators: %s: number of hours */
		return sprintf( __( 'Final reminder sent %s hours after the cart is abandoned', 'easy-digital-downloads' ), absint( $hours / HOUR_IN_SECONDS ) );
	}

	/**
	 * Gets the context label.
	 *
	 * @since 3.6.5
	 * @return string
	 */
	public function get_context_label(): string {
		$delay = $this->get_metadata( 'delay' );

		/* translators: %s: number of hours */
		return sprintf( _n( '%s hour after the cart is abandoned', '%s hours after the cart is abandoned', absint( $delay / HOUR_IN_SECONDS ), 'easy-digital-downloads' ), absint( $delay / HOUR_IN_SECONDS ) );
	}

	/**
	 * Define the default email properties.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	public function defaults(): array {
		return array(
			'subject' => __( 'Your cart is about to expire', 'easy-digital-downloads' ),
			'heading' => __( 'Your Cart Expires Soon', 'easy-digital-downloads' ),
			'content' => $this->get_default_content(),
			'status'  => 1,
		);
	}

	/**
	 * Gets the default email body content.
	 *
	 * @since 3.6.5
	 * @return string
	 */
	protected function get_default_content(): string {
		$colors = \EDD\Utils\Colors::get_button_colors();

		$content = '<p>' . sprintf(
			/* translators: %s: Customer first name */
			__( 'Hi %s,', 'easy-digital-downloads' ),
			'{customer_firstname}'
		) . '</p>';

		$content .= '<p>' . __( 'Just a heads up — the items in your cart won\'t be saved much longer. After your cart expires, you would need to start your purchase over.', 'easy-digital-downloads' ) . '</p>';

		$content .= '<h3>' . __( 'Your Cart', 'easy-digital-downloads' ) . '</h3>';
		$content .= '{cart_items}';

		$content .= '<p><strong>' . __( 'Total:', 'easy-digital-downloads' ) . '</strong> {cart_total}</p>';

		$content .= '<p>' . __( 'If anything is holding you back, we\'d love to help. Just reply to this email with any questions.', 'easy-digital-downloads' ) . '</p>';

		$content .= '<p><a href="{cart_link}" class="edd-button" style="display: inline-block; padding: 12px 24px; background-color: ' . $colors['buttonColor'] . '; color: ' . $colors['buttonTextColor'] . '; text-decoration: none; border-radius: 4px; font-weight: bold;">' . __( 'Complete Your Purchase', 'easy-digital-downloads' ) . '</a></p>';

		$content .= '<p>' . __( 'Best regards,', 'easy-digital-downloads' ) . '<br>';
		$content .= '{sitename}</p>';

		$content .= '<hr style="border: 1px solid #eee; margin: 20px 0;">';
		$content .= '<p class="edd-acr__notice--unsubscribe" style="font-size: 12px; color: #666;">';
		$content .= sprintf(
			/* translators: 1: Unsubscribe link opening tag, 2: Closing tag */
			__( 'This is your final cart recovery reminder. No further emails will be sent about this cart. If you already completed your purchase, you can ignore this email. To stop receiving these reminders in the future, you can %1$sunsubscribe here%2$s.', 'easy-digital-downloads' ),
			'<a href="{unsubscribe_link}">',
			'</a>'
		);
		$content .= '</p>';

		return $content;
	}
}
