<?php
/**
 * Back-in-Stock subscription confirmation email.
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

namespace Kramar\Modules\Marketing;

defined( 'ABSPATH' ) || exit;

/**
 * WooCommerce email sent immediately after a customer signs up for a
 * back-in-stock alert, confirming the request and giving them a one-click
 * cancel link.
 *
 * Registered via `woocommerce_email_classes` so it appears in WC → Settings →
 * Emails and works with Kramar's Email Manager block template pipeline.
 *
 * @since 1.0.0
 */
class Email_Subscription_Confirmed extends \WC_Email {

	/** @since 1.0.0 @var string */
	private string $kramar_product_name = '';

	/** @since 1.0.0 @var string */
	private string $kramar_product_url = '';

	/** @since 1.0.0 @var string */
	private string $kramar_unsub_link = '';

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->id             = 'kramar_bis_confirmed';
		$this->title          = __( 'Back in Stock — Signup Confirmation', 'kramar' );
		$this->description    = __( 'Sent immediately when a customer signs up to be notified that a product is back in stock. Confirms their request and includes a one-click cancel link.', 'kramar' );
		$this->customer_email = true;
		$this->template_html  = '';
		$this->template_plain = '';

		parent::__construct();
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_default_subject(): string {
		/* translators: {product_name}: product name */
		return __( 'You\'re on the list for {product_name}', 'kramar' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_default_heading(): string {
		return __( 'We\'ll let you know when it\'s back', 'kramar' );
	}

	/**
	 * Send the subscription confirmation email.
	 *
	 * @since 1.0.0
	 *
	 * @param string      $recipient  Subscriber email address.
	 * @param \WC_Product $product    Product the customer subscribed to.
	 * @param string      $unsub_link One-click HMAC cancel link.
	 */
	public function trigger( string $recipient, \WC_Product $product, string $unsub_link ): void {
		if ( ! $this->is_enabled() || ! is_email( $recipient ) ) {
			return;
		}

		$this->setup_locale();

		$this->recipient         = $recipient;
		$this->object            = $product;
		$this->kramar_product_name = $product->get_name();
		$this->kramar_product_url  = $product->get_permalink();
		$this->kramar_unsub_link   = $unsub_link;

		$subject = str_replace( '{product_name}', $this->kramar_product_name, $this->get_subject() );
		$content = $this->build_content( $this->kramar_product_name, $this->kramar_product_url, $unsub_link );

		$this->send( $recipient, $subject, $content, $this->get_headers(), $this->get_attachments() );

		$this->restore_locale();
	}

	/**
	 * Provide the merge context for Kramar's Email Manager block pipeline.
	 *
	 * @since 1.0.0
	 *
	 * @return array<string, mixed> Data context: [ 'merge' => [ tag => value ] ].
	 */
	public function kramar_block_context(): array {
		return [
			'merge' => [
				'{product_name}'    => $this->kramar_product_name,
				'{product_url}'     => $this->kramar_product_url,
				'{unsubscribe_url}' => $this->kramar_unsub_link,
			],
		];
	}

	/**
	 * Build the fallback email HTML body (used when the Email Manager is off).
	 *
	 * @since 1.0.0
	 *
	 * @param string $product_name Product display name.
	 * @param string $product_url  Product page permalink.
	 * @param string $unsub_link   Cancel/unsubscribe URL.
	 * @return string HTML content.
	 */
	private function build_content( string $product_name, string $product_url, string $unsub_link ): string {
		ob_start();

		wc_get_template( 'emails/email-header.php', [ 'email_heading' => $this->get_heading() ] );
		?>
		<p>
			<?php
			printf(
				/* translators: %s: product name as link */
				esc_html__( 'Thanks! We\'ll email you as soon as %s is back in stock.', 'kramar' ),
				'<a href="' . esc_url( $product_url ) . '">' . esc_html( $product_name ) . '</a>'
			);
			?>
		</p>
		<p><?php esc_html_e( 'No action is needed from you. If you change your mind, you can cancel this alert at any time:', 'kramar' ); ?></p>
		<p>
			<a href="<?php echo esc_url( $unsub_link ); ?>" style="display:inline-block;padding:.75em 1.5em;background:#000;color:#fff;text-decoration:none;border-radius:3px;">
				<?php esc_html_e( 'Cancel this alert', 'kramar' ); ?>
			</a>
		</p>
		<?php

		wc_get_template( 'emails/email-footer.php' );

		return (string) ob_get_clean();
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_content_html(): string {
		return '';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_content_plain(): string {
		return '';
	}
}
