<?php
namespace Gigneo\Emails;

use WC_Email;

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

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

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_job_cancel_response';
		$this->title          = esc_html__( 'Gigneo - Job order cancel response', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the user when a cancellation request is accepted or rejected.', 'gigneo' );
		$this->template_html  = 'email/job-order-cancel-response.php';
		$this->template_plain = 'email/plain/job-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',
			'{job_title}'      => 'Professional Web Developer',
			'{job_id}'         => '123',
			'{order_id}'       => '456',
			'{status}'         => 'accepted',
			'{order_link}'     => '#',
		);
		parent::__construct();
	}

	/**
	 * Trigger the email when Order cancellation is responded to.
	 *
	 * @param int    $recipient_id Recipient ID.
	 * @param int    $order_id     Order ID.
	 * @param string $status       New status (accepted/rejected).
	 *
	 * @since 0.0.1
	 */
	public function trigger( $recipient_id, $order_id, $status = 'accepted' ) {
		$order_id     = absint( $order_id );
		$recipient_id = absint( $recipient_id );
		$user_id      = get_post_field( 'post_author', $recipient_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_id === $employer_profile_id ) ? $freelancer_profile_id : $employer_profile_id;

		$job_id     = gigneo_post_meta( $order_id, '_gigneo_pm_job_id' );
		$job_title  = gigneo_post_meta( $order_id, '_gigneo_pm_job_title' );
		$order_link = gigneo_dashboard_url( 'order', 'job-detail', array( 'p_id' => $order_id ) );

		if ( $user ) {
			$this->recipient                        = $user->user_email;
			$this->placeholders['{recipient_name}'] = gigneo_profile_name( $recipient_id );
			$this->placeholders['{responder_name}'] = gigneo_profile_name( $responder_id );
			$this->placeholders['{job_title}']      = $job_title;
			$this->placeholders['{job_id}']         = $job_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 job cancellation request - {job_title}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Cancellation request response', '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}'],
				'job_title'        => $this->placeholders['{job_title}'],
				'job_id'           => $this->placeholders['{job_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}'],
				'job_title'        => $this->placeholders['{job_title}'],
				'job_id'           => $this->placeholders['{job_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
		);
	}
}
