<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles email verification email notifications.
 *
 * @since 0.0.1
 */
class Email_Verification extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_email_verification';
		$this->title          = esc_html__( 'Gigneo - Email verification', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to users to verify their email address during registration.', 'gigneo' );
		$this->template_html  = 'email/email-verification.php';
		$this->template_plain = 'email/plain/email-verification.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{user_name}'   => 'John Doe',
			'{verify_link}' => site_url( '#verification_link' ),
		);

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

	/**
	 * Prepares placeholders and sends the email when enabled.
	 *
	 * @since 0.0.1
	 *
	 * @param mixed $user_id WordPress user ID.
	 *
	 * @param mixed $verification_link URL the user follows to verify email.
	 */
	public function trigger( $user_id, $verification_link ) {
		if ( $user_id ) {
			$user = get_userdata( $user_id );
			if ( $user ) {
				$this->recipient                     = $user->user_email;
				$this->placeholders['{user_name}']   = $user->user_login;
				$this->placeholders['{verify_link}'] = $verification_link;
				$this->placeholders['{site_title}']  = get_bloginfo( 'name' );
				// 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}] Verify your email', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Verify your email', '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}'],
				'verify_link'   => $this->placeholders['{verify_link}'],
				'site_title'    => $this->placeholders['{site_title}'],
				'sent_to_admin' => false,
				'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}'],
				'verify_link'   => $this->placeholders['{verify_link}'],
				'site_title'    => $this->placeholders['{site_title}'],
				'sent_to_admin' => false,
				'plain_text'    => true,
				'email'         => $this,
			),
			'',
			$this->template_base
		);
	}
}
