<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Class Service_Review
 *
 * Sends an email when a service order has a review.
 *
 * @since 0.0.1
 */
class Service_Review extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_service_review';
		$this->title          = esc_html__( 'Gigneo - Service order review', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the user when the employer or freelancer reviews an order.', 'gigneo' );
		$this->template_html  = 'email/service-order-review.php';
		$this->template_plain = 'email/plain/service-order-review.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;

		$this->placeholders = array(
			'{profile_name}'           => 'John Doe',
			'{participant_name}'       => 'Jane Smith',
			'{service_title}'          => 'Professional Web Development',
			'{service_id}'             => '123',
			'{order_id}'               => '456',
			'{order_link}'             => '#',
			'{review_comment}'         => 'Great work!',
			'{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 profile ID.
	 * @param int $order_id   Service 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;
		}
		// Get both participants.
		$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' ) );

		// Determine who was reviewed (the receiver).
		$participant_id = ( $profile_id === $employer_profile_id )
			? $freelancer_profile_id
			: $employer_profile_id;

		// Get review comment depending on who wrote it.
		$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_comment = get_comment_meta( $freelancer_review_id, '_gigneo_cm_review_info', true );
		} else {
			// Freelancer reviewed employer.
			$review_comment = get_comment_meta( $employer_review_id, '_gigneo_cm_employer_review_info', true );

		}
		$review_data = is_array( $review_comment ) ? $review_comment : array();
		// Get the receiver user.
		$participant_user = get_userdata( get_post_field( 'post_author', $participant_id ) );

		$review_comment_text    = $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;

		$reviewer_label = gigneo_freelancer_singular();
		$reviewer_type  = gigneo_profile_type( $participant_id ) === 'employer' ? 'freelancer' : 'employer';

		if ( $profile_id === $employer_profile_id ) {
			$review_rating  = round( ( $review_communication + $review_quality_work + $review_understanding ) / 3, 1 );
			$reviewer_label = gigneo_employer_singular();
		}

		if ( ! $participant_user ) {
			return;
		}
		$this->recipient = $participant_user->user_email;
		// Prepare placeholders.
		$this->placeholders['{profile_name}']     = gigneo_profile_name( $profile_id ); // reviewer
		$this->placeholders['{participant_name}'] = gigneo_profile_name( $participant_id ); // receiver
		$this->placeholders['{order_id}']         = $order_id;
		$this->placeholders['{service_id}']       = gigneo_post_meta( $order_id, '_gigneo_pm_service_id' );
		$this->placeholders['{service_title}']    = gigneo_post_meta( $order_id, '_gigneo_pm_service_title' );
		$this->placeholders['{order_link}']       = gigneo_dashboard_url( 'order', 'service-detail', array( 'p_id' => $order_id ) );
		$this->placeholders['{review_comment}']   = $review_comment_text;
		$this->placeholders['{rating}']           = $review_rating;

		// Add breakdown to placeholders
		$this->placeholders['{review_communication}']   = $review_communication ?? 0;
		$this->placeholders['{review_clarity}']         = $review_clarity;
		$this->placeholders['{review_professionalism}'] = $review_professionalism;
		$this->placeholders['{review_quality_work}']    = $review_quality_work;
		$this->placeholders['{review_understanding}']   = $review_understanding;
		$this->placeholders['{reviewer_label}']         = $reviewer_label;
		$this->placeholders['{reviewer_type}']          = $reviewer_type;

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

	/**
	 * Default email subject line before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_subject() {
		return esc_html__( '{site_title} - {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(),
				'email'                  => $this,
				'profile_name'           => $this->placeholders['{profile_name}'],
				'order_id'               => $this->placeholders['{order_id}'],
				'service_id'             => $this->placeholders['{service_id}'],
				'participant_name'       => $this->placeholders['{participant_name}'],
				'service_title'          => $this->placeholders['{service_title}'],
				'order_link'             => $this->placeholders['{order_link}'],
				'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}'],
				'reviewer_label'         => $this->placeholders['{reviewer_label}'],
				'reviewer_type'          => $this->placeholders['{reviewer_type}'],
			),
			'',
			$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}'],
				'order_id'               => $this->placeholders['{order_id}'],
				'service_id'             => $this->placeholders['{service_id}'],
				'participant_name'       => $this->placeholders['{participant_name}'],
				'service_title'          => $this->placeholders['{service_title}'],
				'order_link'             => $this->placeholders['{order_link}'],
				'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}'],
				'reviewer_label'         => $this->placeholders['{reviewer_label}'],
				'reviewer_type'          => $this->placeholders['{reviewer_type}'],
			),
			'',
			$this->template_base
		);
	}
}
