<?php
/**
 * WC_Email subclass: withdrawal "more information needed" notice to the customer.
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

namespace Kramar\Modules\Orders;

defined( 'ABSPATH' ) || exit;

/**
 * Sent to the customer when the shop needs more information to process their
 * withdrawal/return request.
 *
 * @since 1.0.0
 */
final class Kramar_Email_Withdrawal_Info_Request extends \WC_Email {

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->id          = 'kramar_withdrawal_info_request';
		$this->title       = __( 'Withdrawal — Info Requested (Kramar)', 'kramar' );
		$this->description = __( 'Sent to the customer when the shop needs more information to process their withdrawal/return request.', 'kramar' );

		$this->heading = $this->get_default_heading();
		$this->subject = $this->get_default_subject();

		$this->template_html  = 'emails/customer-processing-order.php';
		$this->template_plain = 'emails/plain/customer-processing-order.php';

		$this->customer_email = true;

		parent::__construct();
	}

	/**
	 * Trigger the email for a given order.
	 *
	 * @since 1.0.0
	 *
	 * @param int            $order_id Order ID.
	 * @param \WC_Order|null $order    Order object.
	 */
	public function trigger( $order_id, $order = null ): void { // phpcs:ignore Squiz.Commenting.FunctionComment
		$this->setup_locale();

		if ( $order_id && ! $order instanceof \WC_Order ) {
			$order = wc_get_order( $order_id );
		}

		if ( ! $order instanceof \WC_Order ) {
			$this->restore_locale();
			return;
		}

		$this->object    = $order;
		$this->recipient = $order->get_billing_email();

		$this->placeholders['{order_number}'] = $order->get_order_number();
		$this->placeholders['{order_date}']   = wc_format_datetime( $order->get_date_created() );
		$this->placeholders['{site_title}']   = $this->get_blogname();

		if ( $this->is_enabled() && $this->get_recipient() ) {
			$this->send(
				$this->get_recipient(),
				$this->get_subject(),
				$this->get_content(),
				$this->get_headers(),
				$this->get_attachments()
			);
		}

		$this->restore_locale();
	}

	/**
	 * Return the HTML email content.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_content_html(): string {
		return wc_get_template_html(
			$this->template_html,
			[
				'order'              => $this->object,
				'email_heading'      => $this->get_heading(),
				'additional_content' => $this->get_additional_content_text(),
				'sent_to_admin'      => false,
				'plain_text'         => false,
				'email'              => $this,
			]
		);
	}

	/**
	 * Return the plain-text email content.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_content_plain(): string {
		return wc_get_template_html(
			$this->template_plain,
			[
				'order'              => $this->object,
				'email_heading'      => $this->get_heading(),
				'additional_content' => $this->get_additional_content_text(),
				'sent_to_admin'      => false,
				'plain_text'         => true,
				'email'              => $this,
			]
		);
	}

	/**
	 * Additional content appended after the order table.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_additional_content_text(): string {
		$text = __( 'We need a little more information before we can process your withdrawal request.', 'kramar' );

		if ( $this->object instanceof \WC_Order ) {
			$message = $this->object->get_meta( '_kramar_return_info_message' );
			if ( $message ) {
				$text .= "\n\n" . $message;
			}
			/* translators: %s: shop URL */
			$text .= "\n\n" . sprintf( __( 'Please reply to this email or contact us: %s', 'kramar' ), wc_get_page_permalink( 'shop' ) );
		}

		return $text;
	}

	/**
	 * Default subject line.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_default_subject(): string {
		return __( 'More information needed for your withdrawal (order #{order_number})', 'kramar' );
	}

	/**
	 * Default email heading.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_default_heading(): string {
		return __( 'We need a bit more information', 'kramar' );
	}
}
