<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles service rejected email notifications.
 *
 * @since 0.0.1
 */
class Service_Rejected extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_service_rejected';
		$this->title          = esc_html__( 'Gigneo - Service rejected', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the user when their service is rejected by admin.', 'gigneo' );
		$this->template_html  = 'email/service-rejected-user.php';
		$this->template_plain = 'email/plain/service-rejected-user.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{recipient_name}' => 'John Doe',
			'{service_title}'  => 'Professional Web Development',
			'{service_id}'     => '123',
			'{service_link}'   => '#',
			'{reject_reason}'  => 'The service description does not meet our quality guidelines.',
		);
		parent::__construct();
	}

	/**
	 * Trigger the email to user when their service is rejected.
	 *
	 * @param $recipient_id
	 * @param int $service_id Service ID
	 *
	 * @since 0.0.1
	 */

	public function trigger( $recipient_id, $service_id ) {

		$service_id       = absint( $service_id );
		$recipient_id     = absint( $recipient_id );
		$user_id          = get_post_field( 'post_author', $recipient_id );
		$user             = get_userdata( $user_id );
		$manual_approval  = gigneo_post_meta( $service_id, '_gigneo_pm_manual_approval_info' );
		$rejection_type   = $manual_approval['rejection']['type'] ?? '';
		$rejection_type   = 'soft' === $rejection_type ? esc_html__( 'Soft rejection', 'gigneo' ) : esc_html__( 'Hard rejection', 'gigneo' );
		$rejection_detail = $manual_approval['rejection']['detail'] ?? '';

		if ( $user && $service_id ) {
			$this->recipient                        = $user->user_email;
			$this->placeholders['{recipient_name}'] = gigneo_profile_name( $recipient_id );
			$this->placeholders['{service_title}']  = get_the_title( $service_id );
			$this->placeholders['{service_id}']     = $service_id;
			$this->placeholders['{service_link}']   = get_permalink( $service_id );
			$this->placeholders['{reject_reason}']  = $rejection_detail ?? '';
			$this->placeholders['{reject_type}']    = $rejection_type;

			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__( 'Your service has been rejected - {service_title}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Service rejected', '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}'],
				'service_title'    => $this->placeholders['{service_title}'],
				'service_id'       => $this->placeholders['{service_id}'],
				'service_link'     => $this->placeholders['{service_link}'],
				'reject_reason'    => $this->placeholders['{reject_reason}'],
				'reject_type'      => $this->placeholders['{reject_type}'],
				'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}'],
				'service_title'    => $this->placeholders['{service_title}'],
				'service_id'       => $this->placeholders['{service_id}'],
				'service_link'     => $this->placeholders['{service_link}'],
				'reject_reason'    => $this->placeholders['{reject_reason}'],
				'reject_type'      => $this->placeholders['{reject_type}'],
				'freelancer_label' => gigneo_freelancer_singular(),
				'plain_text'       => true,
				'sent_to_admin'    => false,
				'email'            => $this,
			),
			'',
			$this->template_base
		);
	}
}
