<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Class Job_Published
 * Sends an email notification to admin when an employer posts a new job.
 *
 * @since 0.0.1
 */
class Job_Published extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_job_published';
		$this->title          = esc_html__( 'Gigneo - Job posted', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to admin when an employer posts a new job.', 'gigneo' );
		$this->template_html  = 'email/job-posted-admin.php';
		$this->template_plain = 'email/plain/job-posted-admin.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = false;
		$this->placeholders   = array(
			'{profile_name}' => 'John Doe',
			'{job_title}'    => 'Professional Web Developer',
			'{job_id}'       => '123',
			'{job_link}'     => '#',
			'{site_title}'   => get_bloginfo( 'name' ),
		);
		parent::__construct();
	}
	/**
	 * Trigger the email to admin when a job is posted.
	 *
	 * @param int $employer_profile_id Employer Profile ID (job author).
	 * @param int $job_id              Job ID.
	 *
	 * @since 0.0.1
	 */
	public function trigger( $employer_profile_id, $job_id ) {
		if ( $job_id ) {
			$job           = get_post( $job_id );
			$admin_email   = get_option( 'admin_email' );
			$job_title     = get_the_title( $job_id );
			$job_link      = get_permalink( $job_id );
			$employer_name = gigneo_profile_name( $employer_profile_id );
			if ( $employer_profile_id && $job ) {
				$this->recipient                      = $admin_email ?? '';
				$this->placeholders['{profile_name}'] = $employer_name ?? '';
				$this->placeholders['{job_title}']    = $job_title ?? '';
				$this->placeholders['{job_link}']     = $job_link ?? '';
				$this->placeholders['{site_title}']   = get_bloginfo( 'name' );
				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__( 'A new job has been posted 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 job posted', '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}'],
				'job_title'     => $this->placeholders['{job_title}'],
				'job_link'      => $this->placeholders['{job_link}'],
				'site_title'    => $this->placeholders['{site_title}'],
				'sent_to_admin' => true,
				'plain_text'    => 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}'],
				'job_title'     => $this->placeholders['{job_title}'],
				'job_link'      => $this->placeholders['{job_link}'],
				'site_title'    => $this->placeholders['{site_title}'],
				'sent_to_admin' => true,
				'plain_text'    => true,
				'email'         => $this,
			),
			'',
			$this->template_base
		);
	}
}
