<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles service requested email notifications.
 *
 * @since 0.0.1
 */
class Service_Requested extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_service_requested';
		$this->title          = esc_html__( 'Gigneo - Service approval required', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to admin when a user posts a new service and requires admin approval.', 'gigneo' );
		$this->template_html  = 'email/service-publish-request-admin.php';
		$this->template_plain = 'email/plain/service-publish-request-admin.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = false;
		$this->recipient      = $this->get_option( 'recipient', get_option( 'admin_email' ) );
		$this->placeholders   = array(
			'{freelancer_name}'     => 'John Doe',
			'{service_title}'       => 'Professional Web Development',
			'{service_id}'          => '123',
			'{service_link}'        => '#',
			'{admin_approval_link}' => '#',
		);
		parent::__construct();
	}
	/**
	 *
	 * Trigger the email to admin when a service is posted and requires approval
	 * @param int $profile_id Freelancer ID (service author)
	 * @param int $service_id Service ID
	 *
	 * @since 0.0.1
	 */
	public function trigger( $profile_id, $service_id ) {
		$service_id = absint( $service_id );
		$profile_id = absint( $profile_id );

		if ( $service_id && $profile_id ) {
			$this->placeholders['{freelancer_name}']     = gigneo_profile_name( $profile_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['{admin_approval_link}'] = admin_url( 'edit.php?post_type=gigneo_service' );

			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 service requires admin approval - {service_title}', 'gigneo' );
	}

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