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

defined( 'ABSPATH' ) || exit;

/**
 * CMBIRD_Email_Customer_Quoted_Order Class
 */
class CMBIRD_Email_Customer_Quoted_Order extends WC_Email {

	/**
	 * @var int Customer user ID, available to email templates.
	 */
	public int $user_id = 0;

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

		add_action( 'woocommerce_order_status_quoted_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 processed!', 'commercebird' );
	}

	/**
	 * Get default email heading
	 *
	 * @return string
	 */
	public function get_default_heading(): string {
		return __( 'Please take a look at our best offer.', '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->user_id                        = $this->object->get_customer_id();
			$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(),
				'user_id'            => $this->user_id,
				'sent_to_admin'      => false,
				'plain_text'         => false,
				'email'              => $this,
			),
			'',
			$this->template_base
		);
	}
}
