<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Class Job_Review
 * Sends an email notification to the user when they receive a review for a job order.
 *
 * @since 0.0.1
 */
class Job_Review extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_job_review';
		$this->title          = esc_html__( 'Gigneo - Job order review received', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the user when they receive a review for a job order.', 'gigneo' );
		$this->template_html  = 'email/job-order-review.php';
		$this->template_plain = 'email/plain/job-order-review.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{profile_name}'           => 'John Doe', // Reviewer
			'{participant_name}'       => 'Jane Smith', // Recipient
			'{job_title}'              => 'Professional Web Developer',
			'{job_id}'                 => '123',
			'{order_id}'               => '456',
			'{order_link}'             => '#',
			'{review_comment}'         => 'Excellent work! Highly recommended.',
			'{review_link}'            => '#',
			'{rating}'                 => '5',
			'{review_clarity}'         => '5',
			'{review_communication}'   => '5',
			'{review_professionalism}' => '5',
			'{review_quality_work}'    => '5',
			'{review_understanding}'   => '5',
			'{reviewer_label}'         => 'Freelancer',
			'{reviewer_type}'          => 'freelancer',
		);
		parent::__construct();
	}

	/**
	 * Trigger the email.
	 *
	 * @param int $profile_id Reviewer (Actor) Profile ID.
	 * @param int $order_id   Order ID.
	 *
	 * @since 0.0.1
	 */
	public function trigger( $profile_id, $order_id ) {
		$order_id   = absint( $order_id );
		$profile_id = absint( $profile_id );

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

		$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' ) );

		$participant_id = ( $profile_id === $employer_profile_id ) ? $freelancer_profile_id : $employer_profile_id;

		$freelancer_review_id = gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_review_activity' );
		$employer_review_id   = gigneo_post_meta( $order_id, '_gigneo_pm_employer_review_activity' );

		if ( $profile_id === $employer_profile_id ) {
			$review_data = get_comment_meta( $freelancer_review_id, '_gigneo_cm_review_info', true );
		} else {
			$review_data = get_comment_meta( $employer_review_id, '_gigneo_cm_employer_review_info', true );
		}

		$review_data      = is_array( $review_data ) ? $review_data : array();
		$participant_user = get_userdata( get_post_field( 'post_author', $participant_id ) );

		$review_comment         = $review_data['feedback'] ?? '';
		$review_communication   = $review_data['communication_level'] ?? 0;
		$review_clarity         = $review_data['clarity_of_requirements'] ?? 0;
		$review_professionalism = $review_data['professionalism'] ?? 0;
		$review_rating          = round( ( $review_communication + $review_clarity + $review_professionalism ) / 3, 1 );

		$review_quality_work  = $review_data['quality_work'] ?? 0;
		$review_understanding = $review_data['understanding'] ?? 0;

		if ( ! $participant_user ) {
			return;
		}

		$job_id    = gigneo_post_meta( $order_id, '_gigneo_pm_job_id' );
		$job_title = gigneo_post_meta( $order_id, '_gigneo_pm_job_title' );

		$this->recipient                          = $participant_user->user_email;
		$this->placeholders['{profile_name}']     = gigneo_profile_name( $profile_id ); // Reviewer
		$this->placeholders['{participant_name}'] = gigneo_profile_name( $participant_id ); // Recipient
		$this->placeholders['{job_title}']        = $job_title;
		$this->placeholders['{job_id}']           = $job_id;
		$this->placeholders['{order_id}']         = $order_id;
		$this->placeholders['{order_link}']       = gigneo_dashboard_url( 'order', 'job-detail', array( 'p_id' => $order_id ) );
		$this->placeholders['{review_comment}']   = $review_comment;
		$this->placeholders['{reviewer_label}']   = gigneo_freelancer_singular();
		$this->placeholders['{reviewer_type}']    = gigneo_profile_type( $participant_id ) === 'employer' ? 'freelancer' : 'employer';

		$this->placeholders['{review_communication}']   = $review_communication;
		$this->placeholders['{review_clarity}']         = $review_clarity;
		$this->placeholders['{review_professionalism}'] = $review_professionalism;

		//for employers reviewing freelancers, we have different criteria
		$this->placeholders['{review_quality_work}']  = $review_quality_work;
		$this->placeholders['{review_understanding}'] = $review_understanding;

		if ( $profile_id === $employer_profile_id ) {
			$review_rating                          = round( ( $review_communication + $review_quality_work + $review_understanding ) / 3, 1 );
			$this->placeholders['{reviewer_label}'] = gigneo_employer_singular();
		}
		$this->placeholders['{rating}'] = $review_rating;

		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__( '{profile_name} reviewed you', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'You’ve received a new review!', '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(),
				'profile_name'           => $this->placeholders['{profile_name}'],
				'participant_name'       => $this->placeholders['{participant_name}'],
				'job_title'              => $this->placeholders['{job_title}'],
				'review_comment'         => $this->placeholders['{review_comment}'],
				'rating'                 => $this->placeholders['{rating}'],
				'review_communication'   => $this->placeholders['{review_communication}'],
				'review_clarity'         => $this->placeholders['{review_clarity}'],
				'review_professionalism' => $this->placeholders['{review_professionalism}'],
				'review_quality_work'    => $this->placeholders['{review_quality_work}'],
				'review_understanding'   => $this->placeholders['{review_understanding}'],
				'job_id'                 => $this->placeholders['{job_id}'],
				'order_id'               => $this->placeholders['{order_id}'],
				'order_link'             => $this->placeholders['{order_link}'],
				'reviewer_label'         => $this->placeholders['{reviewer_label}'],
				'reviewer_type'          => $this->placeholders['{reviewer_type}'],
				'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(),
				'profile_name'           => $this->placeholders['{profile_name}'],
				'participant_name'       => $this->placeholders['{participant_name}'],
				'job_title'              => $this->placeholders['{job_title}'],
				'review_comment'         => $this->placeholders['{review_comment}'],
				'rating'                 => $this->placeholders['{rating}'],
				'review_communication'   => $this->placeholders['{review_communication}'],
				'review_clarity'         => $this->placeholders['{review_clarity}'],
				'review_professionalism' => $this->placeholders['{review_professionalism}'],
				'review_quality_work'    => $this->placeholders['{review_quality_work}'],
				'review_understanding'   => $this->placeholders['{review_understanding}'],
				'job_id'                 => $this->placeholders['{job_id}'],
				'order_id'               => $this->placeholders['{order_id}'],
				'order_link'             => $this->placeholders['{order_link}'],
				'reviewer_label'         => $this->placeholders['{reviewer_label}'],
				'reviewer_type'          => $this->placeholders['{reviewer_type}'],
				'plain_text'             => true,
				'sent_to_admin'          => false,
				'email'                  => $this,
			),
			'',
			$this->template_base
		);
	}
}
