<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles user registration email notifications.
 *
 * @since 0.0.1
 */
class User_Registration extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_user_registration';
		$this->title          = esc_html__( 'Gigneo - New registration', 'gigneo' );
		$this->description    = esc_html__( 'Email sent to the admin when a new user registers.', 'gigneo' );
		$this->template_html  = 'email/user-registration.php';
		$this->template_plain = 'email/plain/user-registration.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->placeholders   = array(
			'{user_name}'    => 'john_doe',
			'{user_email}'   => 'john@gigneo.com',
			'{profile_name}' => 'John Doe',
			'{profile_type}' => gigneo_freelancer_singular(),
			'{user_id}'      => 0,
		);

		// Trigger this email when a new user registers
		//add_action('user_register', array($this, 'trigger') );

		// Call parent constructor
		parent::__construct();

		// Set recipient to the admin email (configurable in settings)
		$this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
	}

	/**
	 * Prepares placeholders and sends the email when enabled.
	 *
	 * @since 0.0.1
	 *
	 * @param mixed $user_id WordPress user ID.
	 */
	public function trigger( $user_id ) {
		if ( $user_id ) {
			$user = get_userdata( $user_id );
			if ( $user ) {
				$profile_id                           = gigneo_profile_id( $user_id );
				$profile_type                         = gigneo_profile_type( $profile_id );
				$this->placeholders['{user_name}']    = $user->user_login;
				$this->placeholders['{user_email}']   = $user->user_email;
				$this->placeholders['{user_id}']      = $user_id;
				$this->placeholders['{profile_name}'] = gigneo_profile_name( $profile_id );
				$this->placeholders['{profile_type}'] = ! empty( $profile_type ) ? $profile_type : '';
				// Send the email if enabled and recipient is set
				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__( '[{site_title}] New user registered', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'New user registered', '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(),
				'user_name'        => $this->placeholders['{user_name}'],
				'user_email'       => $this->placeholders['{user_email}'],
				'user_id'          => $this->placeholders['{user_id}'],
				'profile_name'     => $this->placeholders['{profile_name}'],
				'profile_type'     => $this->placeholders['{profile_type}'],
				'freelancer_label' => gigneo_freelancer_singular(),
				'employer_label'   => gigneo_employer_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(),
				'user_name'        => $this->placeholders['{user_name}'],
				'user_email'       => $this->placeholders['{user_email}'],
				'user_id'          => $this->placeholders['{user_id}'],
				'profile_name'     => $this->placeholders['{profile_name}'],
				'profile_type'     => $this->placeholders['{profile_type}'],
				'freelancer_label' => gigneo_freelancer_singular(),
				'employer_label'   => gigneo_employer_singular(),
				'sent_to_admin'    => true,
				'plain_text'       => true,
				'email'            => $this,
			),
			'',
			$this->template_base
		);
	}
}
