<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles account reactivated email notifications.
 *
 * @since 0.0.1
 */
class Account_Reactivated extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_account_reactivated';
		$this->title          = esc_html__( 'Gigneo - Account reactivated', 'gigneo' );
		$this->description    = esc_html__( 'Email sent to the user when their account is reactivated by an admin.', 'gigneo' );
		$this->template_html  = 'email/account-reactivated.php';
		$this->template_plain = 'email/plain/account-reactivated.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{user_name}'  => 'John Doe',
			'{user_email}' => 'john@gigneo.com',
		);

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

	/**
	 * Builds placeholders from the user and optional data, then sends the email when enabled.
	 *
	 * @since 0.0.1
	 *
	 * @param int   $user_id WordPress user ID whose account was reactivated.
	 * @param array $data    Optional context; keys vary by email.
	 */
	public function trigger( $user_id, $data = array() ) {
		if ( $user_id ) {
			$user = get_userdata( $user_id );
			if ( $user ) {
				$this->recipient                    = $user->user_email;
				$this->placeholders['{user_name}']  = $user->user_login;
				$this->placeholders['{user_email}'] = $user->user_email;
				$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}] Account reactivated', 'gigneo' );
	}

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