<?php
namespace Gigneo\Emails;

use WC_Email;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Service_Moderator_Cancelled
 *
 * Sends an email notification to both the freelancer and employer
 * when a moderator cancels a service order.
 *
 * @since 0.0.1
 */
class Service_Moderator_Cancelled extends WC_Email {

	/**
	 * Constructor.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_service_moderator_cancelled';
		$this->title          = esc_html__( 'Gigneo - Service order cancelled by moderator', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to both the freelancer and employer when a moderator cancels the order.', 'gigneo' );
		$this->template_html  = 'email/service-order-moderator-cancelled.php';
		$this->template_plain = 'email/plain/service-order-moderator-cancelled.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{recipient_name}'  => 'John Doe',
			'{freelancer_name}' => 'John Doe',
			'{employer_name}'   => 'Jane Smith',
			'{service_title}'   => 'Professional Web Development',
			'{service_id}'      => '123',
			'{order_id}'        => '456',
			'{order_link}'      => '#',
		);
		parent::__construct();
	}

	/**
	 * Trigger the email.
	 *
	 * @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 );

		if ( ! $order_id || ! $recipient_profile_id ) {
			return;
		}

		$recipient_user_id = get_post_field( 'post_author', $recipient_profile_id );
		$recipient_user    = get_userdata( $recipient_user_id );
		$recipient_name    = gigneo_profile_name( $recipient_profile_id );

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

		$freelancer_name = gigneo_profile_name( $freelancer_profile_id );
		$employer_name   = gigneo_profile_name( $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 ) );

		if ( ! $recipient_user ) {
			return;
		}

		$this->recipient                         = $recipient_user->user_email;
		$this->placeholders['{recipient_name}']  = $recipient_name;
		$this->placeholders['{freelancer_name}'] = $freelancer_name;
		$this->placeholders['{employer_name}']   = $employer_name;
		$this->placeholders['{service_title}']   = $service_title;
		$this->placeholders['{order_id}']        = $order_id;
		$this->placeholders['{service_id}']      = $service_id;
		$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__( 'Service order cancelled by moderator - {service_title}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Service order cancelled!', '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}'],
				'freelancer_name'  => $this->placeholders['{freelancer_name}'],
				'employer_name'    => $this->placeholders['{employer_name}'],
				'service_title'    => $this->placeholders['{service_title}'],
				'service_id'       => $this->placeholders['{service_id}'],
				'order_id'         => $this->placeholders['{order_id}'],
				'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}'],
				'freelancer_name'  => $this->placeholders['{freelancer_name}'],
				'employer_name'    => $this->placeholders['{employer_name}'],
				'service_title'    => $this->placeholders['{service_title}'],
				'service_id'       => $this->placeholders['{service_id}'],
				'order_id'         => $this->placeholders['{order_id}'],
				'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
		);
	}
}
