<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Class Job_Proposal_Submitted
 *
 * Sends an email notification to the employer when a new proposal is submitted for their job.
 *
 * @since 0.0.1
 */
class Job_Proposal_Submitted extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_job_proposal_submitted';
		$this->title          = esc_html__( 'Gigneo - New job proposal', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the Employer when a freelancer submits a new proposal for their job.', 'gigneo' );
		$this->template_html  = 'email/job-proposal-submitted.php';
		$this->template_plain = 'email/plain/job-proposal-submitted.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{recipient_name}'         => 'Jane Smith',
			'{freelancer_name}'        => 'John Doe',
			'{job_title}'              => 'Professional Web Developer',
			'{proposal_id}'            => '123',
			'{job_id}'                 => '456',
			'{proposal_link}'          => '#',
			'{bid_amount}'             => '500',
			'{job_delivery_time}'      => '3',
			'{job_delivery_time_type}' => 'Days',
		);
		parent::__construct();
	}

	/**
	 * Trigger the email to employer when a new proposal is submitted.
	 *
	 * @param int $employer_profile_id Employer profile ID to receive the email.
	 * @param int $proposal_id Proposal ID.
	 *
	 * @since 0.0.1
	 */
	public function trigger( $employer_profile_id, $proposal_id ) {
		if ( ! $employer_profile_id || empty( $proposal_id ) ) {
			return;
		}

		$proposal_id            = absint( $proposal_id );
		$employer_profile_id    = absint( $employer_profile_id );
		$employer_user_id       = get_post_field( 'post_author', $employer_profile_id );
		$freelancer_profile_id  = gigneo_post_meta( $proposal_id, '_gigneo_pm_freelancer_profile_id' );
		$job_id                 = gigneo_post_meta( $proposal_id, '_gigneo_pm_job_id' );
		$bid_amount             = gigneo_post_meta( $proposal_id, '_gigneo_pm_budget' );
		$job_delivery_time      = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration' );
		$job_delivery_time_type = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration_type' );

		$employer_user = get_userdata( $employer_user_id );
		if ( ! $employer_user ) {
			return;
		}

		$employer_name   = gigneo_profile_name( $employer_profile_id );
		$freelancer_name = gigneo_profile_name( $freelancer_profile_id );
		$job_title       = get_the_title( $job_id );
		$proposal_link   = gigneo_dashboard_url( 'proposal', 'detail', array( 'p_id' => $proposal_id ) );

		$this->recipient                                = $employer_user->user_email;
		$this->placeholders['{recipient_name}']         = $employer_name;
		$this->placeholders['{freelancer_name}']        = $freelancer_name;
		$this->placeholders['{job_title}']              = $job_title;
		$this->placeholders['{proposal_link}']          = $proposal_link;
		$this->placeholders['{proposal_id}']            = $proposal_id;
		$this->placeholders['{job_id}']                 = $job_id;
		$this->placeholders['{bid_amount}']             = $bid_amount;
		$this->placeholders['{job_delivery_time}']      = $job_delivery_time;
		$this->placeholders['{job_delivery_time_type}'] = $job_delivery_time_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__( 'New Proposal Received for {job_title} on {site_title}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'New proposal received!', '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}'],
				'freelancer_name'        => $this->placeholders['{freelancer_name}'],
				'job_title'              => $this->placeholders['{job_title}'],
				'proposal_id'            => $this->placeholders['{proposal_id}'],
				'job_id'                 => $this->placeholders['{job_id}'],
				'proposal_link'          => $this->placeholders['{proposal_link}'],
				'bid_amount'             => $this->placeholders['{bid_amount}'],
				'job_delivery_time'      => $this->placeholders['{job_delivery_time}'],
				'job_delivery_time_type' => $this->placeholders['{job_delivery_time_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}'],
				'freelancer_name'        => $this->placeholders['{freelancer_name}'],
				'job_title'              => $this->placeholders['{job_title}'],
				'proposal_id'            => $this->placeholders['{proposal_id}'],
				'job_id'                 => $this->placeholders['{job_id}'],
				'proposal_link'          => $this->placeholders['{proposal_link}'],
				'bid_amount'             => $this->placeholders['{bid_amount}'],
				'job_delivery_time'      => $this->placeholders['{job_delivery_time}'],
				'job_delivery_time_type' => $this->placeholders['{job_delivery_time_type}'],
				'freelancer_label'       => gigneo_freelancer_singular(),
				'plain_text'             => true,
				'sent_to_admin'          => false,
				'email'                  => $this,
			),
			'',
			$this->template_base
		);
	}
}
