<?php
namespace Gigneo\Emails;

use WC_Email;

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

/**
 * Handles account closed email notifications.
 *
 * @since 0.0.1
 */
class Account_Closed extends WC_Email {

	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_account_closed';
		$this->title          = esc_html__( 'Gigneo - Account closed', 'gigneo' );
		$this->description    = esc_html__( 'Email sent to the admin when a user permanently closes their account.', 'gigneo' );
		$this->template_html  = 'email/account-closed.php';
		$this->template_plain = 'email/plain/account-closed.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->placeholders   = array(
			'{user_name}'  => 'john_doe',
			'{user_email}' => 'john@gigneo.com',
			'{user_id}'    => 0,
			'{reason}'     => esc_html__( 'Reason', 'gigneo' ),
			'{detail}'     => esc_html__( 'A detailed description of why the user closed their account.', 'gigneo' ),
			'{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' ) );
	}

	/**
	 * 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 closed.
	 * @param array $data    Optional keys: `reason`, `detail` (override stored user meta).
	 */
	public function trigger( $user_id, $data = array() ) {
		if ( $user_id ) {
			$user = get_userdata( $user_id );
			if ( $user ) {
				$date_format                        = get_option( 'date_format' );
				$time_format                        = get_option( 'time_format' );
				$closure_info                       = get_user_meta( $user_id, '_gigneo_um_account_closed_info', true );
				$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 );
				$this->placeholders['{reason}']     = $data['reason'] ?? ( $closure_info['reason'] ?? '' );
				$this->placeholders['{detail}']     = $data['detail'] ?? ( $closure_info['detail'] ?? '' );
				// 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 closed', 'gigneo' );
	}

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