<?php
/**
 * WC_Email subclass: new withdrawal request notification sent to the shop admin.
 *
 * Loaded on demand when WC's mailer initialises, ensuring WC_Email is available.
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

namespace Kramar\Modules\Orders;

// Prevent direct access.
defined( 'ABSPATH' ) || exit;

/**
 * Sent to the shop admin when a customer submits a withdrawal request.
 *
 * @since 1.0.0
 */
final class Kramar_Email_Withdrawal_Request extends \WC_Email {

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$this->id          = 'kramar_withdrawal_request';
		$this->title       = __( 'New Withdrawal Request (Kramar)', 'kramar' );
		$this->description = __( 'Sent to the shop admin when a customer submits a withdrawal request.', 'kramar' );

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

		$this->template_html  = 'emails/admin-new-order.php';
		$this->template_plain = 'emails/plain/admin-new-order.php';

		$this->customer_email = false;
		$this->recipient      = $this->get_option( 'recipient', \Kramar\Modules\Email\Email_Manager::admin_recipient_default() );

		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 = $this->get_option( 'recipient', \Kramar\Modules\Email\Email_Manager::admin_recipient_default() );

		$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 Email HTML.
	 */
	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'      => true,
				'plain_text'         => false,
				'email'              => $this,
			]
		);
	}

	/**
	 * Return the plain-text email content.
	 *
	 * @since 1.0.0
	 *
	 * @return string Plain-text email body.
	 */
	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'      => true,
				'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 = __( 'A customer has submitted a withdrawal request for the following order:', 'kramar' );

		if ( $this->object instanceof \WC_Order ) {
			$reason = $this->object->get_meta( '_kramar_return_reason' );
			if ( $reason ) {
				/* translators: %s: customer-supplied reason for the withdrawal */
				$text .= "\n\n" . sprintf( __( 'Reason: %s', 'kramar' ), $reason );
			}

			$admin_url = admin_url( 'post.php?post=' . $this->object->get_id() . '&action=edit' );
			/* translators: %s: URL to the order edit screen in WP admin */
			$text .= "\n\n" . sprintf( __( 'View order: %s', 'kramar' ), $admin_url );
		}

		return $text;
	}

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

	/**
	 * Default email heading.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_default_heading(): string {
		return __( 'New Withdrawal Request', 'kramar' );
	}
}
