<?php
namespace Gigneo\Emails;

use WC_Email;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Service_Cancel_Response
 * Sends an email notification to the user when service order cancellation request is responded.
 *
 * @since 0.0.1
 */
class Service_Cancel_Response extends WC_Email {

	/**
	 * Constructor.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_service_cancel_response';
		$this->title          = esc_html__( 'Gigneo - Service order cancel response', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the other party when a cancellation request is responded to.', 'gigneo' );
		$this->template_html  = 'email/service-order-cancel-response.php';
		$this->template_plain = 'email/plain/service-order-cancel-response.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{recipient_name}' => 'John Doe',
			'{responder_name}' => 'Jane Smith',
			'{service_title}'  => 'Professional Web Development',
			'{service_id}'     => '123',
			'{order_id}'       => '456',
			'{status}'         => 'Approved',
			'{order_link}'     => '#',
		);
		parent::__construct();
	}

	/**
	 * Trigger the email when Order cancellation request is responded to.
	 *
	 * @param int $recipient_profile_id Recipient Profile ID.
	 * @param int $order_id              Order ID.
	 *
	 * @since 0.0.1
	 */
	public function trigger( $recipient_profile_id, $order_id ) {
		$order_id             = absint( $order_id );
		$recipient_profile_id = absint( $recipient_profile_id );
		$user_id              = get_post_field( 'post_author', $recipient_profile_id );
		$user                 = get_userdata( $user_id );

		$employer_profile_id   = absint( gigneo_post_meta( $order_id, '_gigneo_pm_profile_id' ) );
		$freelancer_profile_id = absint( gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id' ) );

		$responder_id = ( $recipient_profile_id === $employer_profile_id ) ? $freelancer_profile_id : $employer_profile_id;

		$service_id    = gigneo_post_meta( $order_id, '_gigneo_pm_service_id' );
		$service_title = gigneo_post_meta( $order_id, '_gigneo_pm_service_title' );
		$order_link    = gigneo_dashboard_url( 'order', 'service-detail', array( 'p_id' => $order_id ) );
		$status        = ( 'cancelled' === get_post_status( $order_id ) ) ? esc_html__( 'Approved', 'gigneo' ) : esc_html__( 'Declined', 'gigneo' );

		if ( $user ) {
			$this->recipient                        = $user->user_email;
			$this->placeholders['{recipient_name}'] = gigneo_profile_name( $recipient_profile_id );
			$this->placeholders['{responder_name}'] = gigneo_profile_name( $responder_id );
			$this->placeholders['{service_title}']  = $service_title;
			$this->placeholders['{service_id}']     = $service_id;
			$this->placeholders['{order_id}']       = $order_id;
			$this->placeholders['{status}']         = $status;
			$this->placeholders['{order_link}']     = $order_link;

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

	/**
	 * Default email subject line before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_subject() {
		return esc_html__( 'Response to your cancellation request - {service_title}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Cancellation request update', 'gigneo' );
	}

	/**
	 * Renders the HTML email body from the template.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_content_html() {
		return wc_get_template_html(
			$this->template_html,
			array(
				'email_heading'    => $this->get_heading(),
				'recipient_name'   => $this->placeholders['{recipient_name}'],
				'responder_name'   => $this->placeholders['{responder_name}'],
				'service_title'    => $this->placeholders['{service_title}'],
				'service_id'       => $this->placeholders['{service_id}'],
				'order_id'         => $this->placeholders['{order_id}'],
				'status'           => $this->placeholders['{status}'],
				'order_link'       => $this->placeholders['{order_link}'],
				'employer_label'   => gigneo_employer_singular(),
				'freelancer_label' => gigneo_freelancer_singular(),
				'plain_text'       => false,
				'sent_to_admin'    => false,
				'email'            => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Renders the plain-text email body from the template.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_content_plain() {
		return wc_get_template_html(
			$this->template_plain,
			array(
				'email_heading'    => $this->get_heading(),
				'recipient_name'   => $this->placeholders['{recipient_name}'],
				'responder_name'   => $this->placeholders['{responder_name}'],
				'service_title'    => $this->placeholders['{service_title}'],
				'service_id'       => $this->placeholders['{service_id}'],
				'order_id'         => $this->placeholders['{order_id}'],
				'status'           => $this->placeholders['{status}'],
				'order_link'       => $this->placeholders['{order_link}'],
				'employer_label'   => gigneo_employer_singular(),
				'freelancer_label' => gigneo_freelancer_singular(),
				'plain_text'       => true,
				'sent_to_admin'    => false,
				'email'            => $this,
			),
			'',
			$this->template_base
		);
	}
}
