<?php
/**
 * Custom 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

/**
 * Generic, user-creatable Abandoned Cart email template.
 * This is a dynamic email that only saves when someone creates a new one.
 */
class Custom extends Base {

	/**
	 * The array key for the custom email.
	 *
	 * @since 3.6.5
	 * @var string
	 */
	protected $key;

	/**
	 * The email meta.
	 *
	 * @since 3.6.5
	 * @var array
	 */
	protected $meta = array(
		'type'          => 'custom',
		'delay'         => 24 * HOUR_IN_SECONDS,
		'unit'          => HOUR_IN_SECONDS,
		'discount_code' => '',
	);

	/**
	 * Custom constructor.
	 *
	 * @param string $key   The custom email key.
	 * @param object $email The email object.
	 */
	public function __construct( $key = '', $email = null ) {
		parent::__construct( $key, $email );
		$this->key      = str_replace( 'acr-custom-', '', $key );
		$this->email_id = $key;
		if ( 'new' === $this->key ) {
			$this->can_view = false;
		}
	}

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

	/**
	 * Description of the email.
	 *
	 * @since 3.6.5
	 * @return string
	 */
	public function get_description() {
		return __( 'This is a custom Abandoned Cart email. It will send a set number of hours after the previous email.', 'easy-digital-downloads' );
	}

	/**
	 * Gets a custom label for the email context.
	 *
	 * @return string
	 */
	public function get_context_label(): string {
		if ( ! $this->can_view ) {
			return '';
		}

		$hours = absint( $this->get_metadata( 'delay' ) );

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

	/**
	 * Define the default email properties.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	public function defaults(): array {
		return array(
			'subject' => __( 'Complete your purchase – your cart is ready', 'easy-digital-downloads' ),
			'heading' => __( 'We saved your cart', 'easy-digital-downloads' ),
			'content' => $this->get_default_content(),
			'status'  => 0,
		);
	}

	/**
	 * The email properties that can be edited.
	 *
	 * @return array
	 */
	protected function get_editable_properties(): array {
		return array(
			'content',
			'subject',
			'heading',
			'status',
		);
	}

	/**
	 * Gets the row actions for the email.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	public function get_row_actions() {
		if ( ! $this->can_view ) {
			return array();
		}

		$row_actions          = parent::get_row_actions();
		$row_actions['trash'] = array(
			'text' => __( 'Delete', 'easy-digital-downloads' ),
			'url'  => wp_nonce_url(
				add_query_arg(
					array(
						'edd-action' => 'delete_acr_custom_email',
						'email-id'   => urlencode( $this->key ),
					),
				),
				'delete_acr_custom_email'
			),
		);

		return $row_actions;
	}

	/**
	 * Gets the default email body content.
	 *
	 * @since 3.6.5
	 * @return string
	 */
	protected function get_default_content(): string {
		$content  = '<p>' . __( 'Hi {customer_firstname},', 'easy-digital-downloads' ) . '</p>';
		$content .= '<p>' . __( 'Looks like you left something behind. Your cart is still saved and ready whenever you are.', '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 .= $this->get_default_cart_link();
		$content .= '<p>' . __( 'If you already completed your purchase, you can ignore this email.', 'easy-digital-downloads' ) . '</p>';
		$content .= '<p>' . __( 'Best,', 'easy-digital-downloads' ) . '<br>{sitename}</p>';
		$content .= $this->get_unsubscribe_content();

		return $content;
	}
}
