<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles account activated admin notification emails.
 *
 * @since 0.0.1
 */
class Account_Activated extends WC_Email {

	/**
	 * Initialize the email settings and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_account_activated';
		$this->title          = esc_html__( 'Gigneo - Account activated', 'gigneo' );
		$this->description    = esc_html__( 'Email sent to the admin when a user activates their account.', 'gigneo' );
		$this->template_html  = 'email/account-activated.php';
		$this->template_plain = 'email/plain/account-activated.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->placeholders   = array(
			'{user_name}'  => 'john_doe',
			'{user_email}' => 'john@gigneo.com',
			'{user_id}'    => 0,
			'{date_time}'  => current_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
		);

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

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

	/**
	 * Trigger the email after a user account is activated.
	 *
	 * @since 0.0.1
	 *
	 * @param int $user_id User ID.
	 */
	public function trigger( $user_id ) {
		if ( $user_id ) {
			$user = get_userdata( $user_id );
			if ( $user ) {
				$date_format                        = get_option( 'date_format' );
				$time_format                        = get_option( 'time_format' );
				$this->placeholders['{user_name}']  = $user->user_login;
				$this->placeholders['{user_email}'] = $user->user_email;
				$this->placeholders['{user_id}']    = $user->ID;
				$this->placeholders['{site_title}'] = get_bloginfo( 'name' );
				$this->placeholders['{date_time}']  = wp_date( $date_format . ' ' . $time_format );

				// 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()
					);
				}
			}
		}
	}

	/**
	 * Get the default email subject.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_subject() {
		return esc_html__( '[{site_title}] Account activated', 'gigneo' );
	}

	/**
	 * Get the default email heading.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Account activated', 'gigneo' );
	}

	/**
	 * Get HTML email content.
	 *
	 * @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}'],
				'site_title'    => $this->placeholders['{site_title}'],
				'date_time'     => $this->placeholders['{date_time}'],
				'sent_to_admin' => true,
				'plain_text'    => false,
				'email'         => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Get plain text email content.
	 *
	 * @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}'],
				'site_title'    => $this->placeholders['{site_title}'],
				'date_time'     => $this->placeholders['{date_time}'],
				'sent_to_admin' => true,
				'plain_text'    => true,
				'email'         => $this,
			),
			'',
			$this->template_base
		);
	}
}
