<?php
/**
 * Discontinued Product WooCommerce email.
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

namespace Kramar\Modules\Marketing;

defined( 'ABSPATH' ) || exit;

/**
 * WooCommerce email sent to subscribers when a watched product is discontinued.
 *
 * Registered via `woocommerce_email_classes` — appears in WC → Settings →
 * Emails and works with Kramar's Email Manager block template pipeline.
 *
 * @since 1.0.0
 */
class Email_Discontinued extends \WC_Email {

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

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

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->id             = 'kramar_discontinued';
		$this->title          = __( 'Back-in-Stock Request Closed', 'kramar' );
		$this->description    = __( 'Sent to a back-in-stock subscriber when their request is closed — either because the product was removed from the shop, or because it never came back in stock within the configured number of days. Confirms that their request and data have been deleted (GDPR/AVG).', '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 __( 'Update on {product_name}', 'kramar' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_default_heading(): string {
		return __( 'An update about your saved product', 'kramar' );
	}

	/**
	 * 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,
				'{shop_url}'     => '' !== $this->kramar_shop_url ? $this->kramar_shop_url : (string) wc_get_page_permalink( 'shop' ),
			],
		];
	}

	/**
	 * Send the discontinued email.
	 *
	 * @since 1.0.0
	 *
	 * @param string $recipient    Subscriber email address.
	 * @param string $product_name Display name of the discontinued product.
	 * @param string $shop_url     Shop/category browse URL.
	 * @param string $unsub_link   HMAC-signed unsubscribe URL.
	 */
	public function trigger(
		string $recipient,
		string $product_name,
		string $shop_url,
		string $unsub_link
	): void {
		if ( ! $this->is_enabled() || ! is_email( $recipient ) ) {
			return;
		}

		$this->setup_locale();
		$this->recipient         = $recipient;
		$this->kramar_product_name = $product_name;
		$this->kramar_shop_url     = $shop_url;

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

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

		$this->restore_locale();
	}

	/**
	 * Build the email HTML body.
	 *
	 * @since 1.0.0
	 *
	 * @param string $product_name Display name of the discontinued product.
	 * @param string $shop_url     Shop/category browse URL.
	 * @param string $unsub_link   Unsubscribe URL.
	 * @return string HTML content.
	 */
	private function build_content(
		string $product_name,
		string $shop_url,
		string $unsub_link
	): string {
		ob_start();

		$email_heading = $this->get_heading();

		wc_get_template(
			'emails/email-header.php',
			[ 'email_heading' => $email_heading ]
		);
		?>
		<p>
			<?php
			printf(
				/* translators: %s: product name */
				esc_html__( 'We are no longer able to send you a back-in-stock notification for %s.', 'kramar' ),
				'<strong>' . esc_html( $product_name ) . '</strong>'
			);
			?>
		</p>
		<p><?php esc_html_e( 'Because of this, we have closed your request and deleted the email address you gave us, in line with the GDPR / AVG. You do not need to do anything — there is nothing left to unsubscribe from.', 'kramar' ); ?></p>
		<p><?php esc_html_e( 'You may find a great alternative in our shop:', 'kramar' ); ?></p>
		<p>
			<a href="<?php echo esc_url( $shop_url ); ?>" style="display:inline-block;padding:.75em 1.5em;background:#000;color:#fff;text-decoration:none;border-radius:3px;">
				<?php esc_html_e( 'Browse our shop', 'kramar' ); ?>
			</a>
		</p>
		<?php
		unset( $unsub_link );

		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 '';
	}
}
