<?php
/**
 * CommerceBird Email: Customer on-quote order notification
 *
 * @package CommerceBird\Includes\Quotes\Emails
 */

defined( 'ABSPATH' ) || exit;

/**
 * CMBIRD_Email_Customer_OnQuote_Order Class
 */
class CMBIRD_Email_Customer_OnQuote_Order extends WC_Email {

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->id             = 'cmbird_customer_onquote_order';
		$this->customer_email = true;
		$this->title          = __( 'On quote order', 'commercebird' );
		$this->description    = __( 'Send email notification to customer when their quote request is received.', 'commercebird' );
		$this->template_html  = 'emails/customer-onquote-order.php';

		add_action( 'woocommerce_order_status_on-quote_notification', array( $this, 'trigger' ), 10, 2 );

		parent::__construct();

		$this->template_base = CMBIRD_PATH . 'templates/';
	}

	/**
	 * Get default email subject
	 *
	 * @return string
	 */
	public function get_default_subject(): string {
		return __( 'Your quote request on {site_title} is received!', 'commercebird' );
	}

	/**
	 * Get default email heading
	 *
	 * @return string
	 */
	public function get_default_heading(): string {
		return __( 'We are checking your request.', 'commercebird' );
	}

	/**
	 * Trigger the sending of this email
	 *
	 * @param int      $order_id Order ID.
	 * @param WC_Order $order    Order object.
	 */
	public function trigger( int $order_id, $order = false ): void {
		$this->setup_locale();

		if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
			$order = wc_get_order( $order_id );
		}

		if ( is_a( $order, 'WC_Order' ) ) {
			$this->object                         = $order;
			$this->recipient                      = $this->object->get_billing_email();
			$this->placeholders['{order_date}']   = wc_format_datetime( $this->object->get_date_created() );
			$this->placeholders['{order_number}'] = $this->object->get_order_number();
		}

		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();
	}

	/**
	 * Get HTML email content
	 *
	 * @return string
	 */
	public function get_content_html(): string {
		return wc_get_template_html(
			$this->template_html,
			array(
				'order'              => $this->object,
				'email_heading'      => $this->get_heading(),
				'additional_content' => $this->get_additional_content(),
				'blogname'           => $this->get_blogname(),
				'sent_to_admin'      => false,
				'plain_text'         => false,
				'email'              => $this,
			),
			'',
			$this->template_base
		);
	}
}
