<?php
/**
 * The cart recovery email editor.
 *
 * @package EDD\Pro\CartRecovery\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\Emails;

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

use EDD\EventManagement\SubscriberInterface;

/**
 * The cart recovery email editor.
 *
 * @since 3.6.5
 * @package EDD\Pro\CartRecovery\Emails
 */
class Editor implements SubscriberInterface {

	/**
	 * Gets the subscribed events.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	public static function get_subscribed_events(): array {
		return array(
			'edd_email_editor_form'          => 'show_meta_on_editor',
			'edd_email_editor_style_formats' => array( 'register_style_formats', 10, 2 ),
		);
	}

	/**
	 * Adds email meta to the email edit screen.
	 *
	 * @since 3.6.5
	 * @param \EDD\Emails\Templates\Base $email The email object.
	 */
	public function show_meta_on_editor( $email ) {
		if ( 'edd' !== $email->sender || 'abandoned_cart' !== $email->context ) {
			return;
		}

		// Handle custom email type field.
		if ( 0 === strpos( $email->email_id, 'acr-custom-' ) ) {
			?>
			<input type="hidden" name="type" value="custom">
			<?php
		}

		$delay      = $email->get_metadata( 'delay' );
		$unit       = (int) $email->get_metadata( 'unit' );
		$unit_label = __( 'hours', 'easy-digital-downloads' );
		if ( MINUTE_IN_SECONDS === $unit ) {
			$unit_label = __( 'minutes', 'easy-digital-downloads' );
		}
		$delay_in_units = $delay / $unit;
		?>
		<div class="edd-form-group">
			<label for="edd-acr-delay" class="edd-form-group__label"><?php esc_html_e( 'Email Delay', 'easy-digital-downloads' ); ?></label>
			<div class="edd-form-group__control">
				<?php
				$amount_type = new \EDD\HTML\AmountType(
					array(
						'name'        => 'delay',
						'id'          => 'edd-acr-delay',
						'value'       => $delay_in_units,
						'min'         => 1,
						'max'         => 72,
						'unit'        => $unit_label,
						'placeholder' => $delay_in_units,
					)
				);
				$amount_type->output();
				?>
				<p class="edd-form-group__description">
					<?php
					printf(
						/* translators: 1: unit, 2: email name */
						esc_html__( 'Wait this many %1$s after cart abandonment before sending the %2$s email.', 'easy-digital-downloads' ),
						esc_html( $unit_label ),
						esc_html( $email->get_name() )
					);
					?>
				</p>
			</div>
		</div>
		<div class="edd-form-group">
			<label for="edd-acr-discount" class="edd-form-group__label"><?php esc_html_e( 'Discount Code', 'easy-digital-downloads' ); ?></label>
			<div class="edd-form-group__control">
				<?php
				$discount_select = new \EDD\HTML\DiscountSelect(
					array(
						'name'              => 'discount_code',
						'id'                => 'edd-acr-discount',
						'selected'          => $email->get_metadata( 'discount_code' ),
						'chosen'            => true,
						'show_option_empty' => __( 'Honor saved cart discount', 'easy-digital-downloads' ),
						'status'            => array( 'active' ),
						'filter_invalid'    => true,
					)
				);
				$discount_select->output();
				?>
				<p class="edd-form-group__description">
					<?php esc_html_e( 'Apply a discount automatically when the customer restores their cart. Keep their existing discount or replace it with your selection.', 'easy-digital-downloads' ); ?>
				</p>
			</div>
		</div>
		<?php
	}

	/**
	 * Registers ACR style formats with the email editor when the context is abandoned cart.
	 *
	 * @since 3.6.5
	 * @param array                      $formats Existing format definitions.
	 * @param \EDD\Emails\Templates\Base $email   The email object.
	 * @return array Merged format definitions.
	 */
	public function register_style_formats( $formats, $email ) {
		if ( 'abandoned_cart' !== $email->context ) {
			return $formats;
		}

		return array_merge( $formats, $this->get_style_formats() );
	}

	/**
	 * Returns the style format definitions for abandoned cart recovery emails.
	 *
	 * @since 3.6.5
	 * @return array Format definitions (TinyMCE style_formats shape).
	 */
	private function get_style_formats() {
		return array(
			'acr_urgent_notice'      => array(
				'title'       => __( 'Urgent Notice', 'easy-digital-downloads' ),
				'description' => __( 'A notice that the cart is at an urgent stage.', 'easy-digital-downloads' ),
				'block'       => 'div',
				'classes'     => 'edd-acr__notice--urgent',
				'wrapper'     => true,
				'styles'      => array(
					'background-color' => '#fff3cd',
					'border'           => '1px solid #ffeaa7',
					'padding'          => '15px',
					'margin'           => '0 0 20px 0',
					'border-radius'    => '4px',
					'color'            => '#856404',
				),
			),
			'acr_final_notice'       => array(
				'title'       => __( 'Final Notice', 'easy-digital-downloads' ),
				'description' => __( 'A notice that the cart is at a final stage and needs to be completed soon.', 'easy-digital-downloads' ),
				'block'       => 'div',
				'classes'     => 'edd-acr__notice--final',
				'wrapper'     => true,
				'styles'      => array(
					'background-color' => '#f8d7da',
					'border'           => '1px solid #f5c6cb',
					'padding'          => '15px',
					'margin'           => '20px 0',
					'border-radius'    => '4px',
					'color'            => '#721c24',
				),
			),
			'acr_unsubscribe_notice' => array(
				'title'       => __( 'Unsubscribe Notice', 'easy-digital-downloads' ),
				'description' => __( 'Subtle text that encourages the customer to unsubscribe from the recovery email.', 'easy-digital-downloads' ),
				'block'       => 'p',
				'classes'     => 'edd-acr__notice--unsubscribe',
				'styles'      => array(
					'font-size' => '12px',
					'color'     => '#666666',
				),
			),
			'acr_urgent_button'      => array(
				'title'       => __( 'Urgent Button', 'easy-digital-downloads' ),
				'description' => __( 'Show a link as a button that encourages the customer to complete their purchase.', 'easy-digital-downloads' ),
				'selector'    => 'a',
				'classes'     => 'edd-acr__button--urgent',
				'styles'      => array(
					'display'          => 'inline-block',
					'padding'          => '15px 30px',
					'background-color' => '#dc3545',
					'color'            => '#ffffff',
					'text-decoration'  => 'none',
					'border-radius'    => '4px',
					'font-weight'      => 'bold',
					'font-size'        => '16px',
				),
			),
		);
	}
}
