<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles service approved email notifications.
 *
 * @since 0.0.1
 */
class Service_Approved extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_service_approved';
		$this->title          = esc_html__( 'Gigneo - Service approved', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the user when their service is approved by admin.', 'gigneo' );
		$this->template_html  = 'email/service-approved-user.php';
		$this->template_plain = 'email/plain/service-approved-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}'   => '#',
		);
		parent::__construct();
	}

	/**
	 * Prepares placeholders and sends the email when enabled.
	 *
	 * @since 0.0.1
	 *
	 * @param mixed $recipient_id Profile or user identifier for the recipient.
	 *
	 * @param mixed $service_id Service post ID.
	 */
	public function trigger( $recipient_id, $service_id ) {
		$service_id    = absint( $service_id );
		$service_title = get_the_title( $service_id );
		$recipient_id  = absint( $recipient_id );
		$user_id       = get_post_field( 'post_author', $recipient_id );
		$user          = get_userdata( $user_id );

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

			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 approved - {service_title}', 'gigneo' );
	}

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