<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Class Job_Rejected
 * Sends an email notification to the employer when their job is rejected by admin.
 *
 * @since 0.0.1
 */
class Job_Rejected extends WC_Email {


	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_job_rejected';
		$this->title          = esc_html__( 'Gigneo - Job rejected', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the employer when their job is rejected by admin.', 'gigneo' );
		$this->template_html  = 'email/job-rejected-user.php';
		$this->template_plain = 'email/plain/job-rejected-user.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{recipient_name}' => 'John Doe',
			'{job_title}'      => 'Professional Web Developer',
			'{reject_reason}'  => 'Missing required skills list.',
		);
		parent::__construct();
	}

	/**
	 * Trigger the email to employer when their job is rejected.
	 *
	 * @param int $employer_profile_id Employer Profile ID (recipient).
	 * @param int $job_id              Job ID.
	 *
	 * @since 0.0.1
	 */

	public function trigger( $employer_profile_id, $job_id ) {

		$job_id           = absint( $job_id );
		$employer_user_id = get_post_field( 'post_author', $employer_profile_id );
		$employer         = get_userdata( $employer_user_id );
		$manual_approval  = gigneo_post_meta( $job_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 ( $employer && $job_id ) {
			$this->recipient                        = $employer->user_email;
			$this->placeholders['{recipient_name}'] = gigneo_profile_name( $employer_profile_id );
			$this->placeholders['{job_title}']      = get_the_title( $job_id );
			$this->placeholders['{job_id}']         = $job_id;
			$this->placeholders['{job_link}']       = get_permalink( $job_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 job has been rejected on {site_title}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Job 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}'],
				'job_title'      => $this->placeholders['{job_title}'],
				'job_id'         => $this->placeholders['{job_id}'],
				'job_link'       => $this->placeholders['{job_link}'],
				'reject_reason'  => $this->placeholders['{reject_reason}'],
				'reject_type'    => $this->placeholders['{reject_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(),
				'recipient_name' => $this->placeholders['{recipient_name}'],
				'job_title'      => $this->placeholders['{job_title}'],
				'job_id'         => $this->placeholders['{job_id}'],
				'job_link'       => $this->placeholders['{job_link}'],
				'reject_reason'  => $this->placeholders['{reject_reason}'],
				'reject_type'    => $this->placeholders['{reject_type}'],
				'plain_text'     => true,
				'sent_to_admin'  => false,
				'email'          => $this,
			),
			'',
			$this->template_base
		);
	}
}
