<?php
/**
 * Account New Customer Email
 *
 * An email sent to customers when their account is flagged for deletion due to being a new customer without orders.
 *
 * @class       Wuclean_WC_Account_New_Customer_Email
 * @extends     WC_Email
 * @package     Wuclean
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Wuclean_WC_Account_New_Customer_Email class
 */
class Wuclean_WC_Account_New_Customer_Email extends WC_Email {

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->id             = 'wuclean_account_new_customer';
		$this->customer_email = true;
		$this->title          = __( 'New customer (no orders)', 'wuclean-database-cleaner-for-woocommerce' );
		$this->description    = __( 'Sent to customers when their account is flagged for deletion due to being a new customer without orders. Available placeholders: {site_title}, {site_url}, {customer_name}, {customer_email}, {days_until_deletion}, {shop_url}. For full template customization, copy the template to your theme\'s woocommerce/emails/ folder.', 'wuclean-database-cleaner-for-woocommerce' );
		$this->template_html  = 'customer-account-inactivity.php';
		$this->template_plain = 'plain/customer-account-inactivity.php';
		$this->template_base  = plugin_dir_path( __DIR__ ) . 'emails/';

		// Set default placeholders (used for preview and actual emails).
		$this->placeholders = array(
			'{site_title}'          => $this->get_blogname(),
			'{site_url}'            => wp_parse_url( home_url(), PHP_URL_HOST ),
			'{customer_name}'       => __( 'John Doe', 'wuclean-database-cleaner-for-woocommerce' ),
			'{customer_email}'      => 'customer@example.com',
			'{days_until_deletion}' => ' 7', // Note: includes leading space and full phrase for conditional formatting.
			'{shop_url}'            => home_url(),
		);

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

	/**
	 * Get email subject
	 */
	public function get_default_subject() {
		return __( 'Complete Your First Order to Keep Your Account Active', 'wuclean-database-cleaner-for-woocommerce' );
	}

	/**
	 * Get email heading
	 */
	public function get_default_heading() {
		return __( 'Complete Your First Order', 'wuclean-database-cleaner-for-woocommerce' );
	}

	/**
	 * Trigger the sending of this email
	 *
	 * @param int $user_id User ID.
	 * @param int $days_until_deletion Days until auto-deletion (0 if manual).
	 */
	public function trigger( $user_id, $days_until_deletion = 0 ) {
		$this->setup_locale();

		$user = get_userdata( $user_id );
		if ( ! $user ) {
			$this->restore_locale();
			return;
		}

		$this->object    = $user;
		$this->recipient = $user->user_email;

		// Add custom placeholders - these can be used in subject, heading, and additional content.
		$this->placeholders['{customer_name}']  = $user->display_name;
		$this->placeholders['{customer_email}'] = $user->user_email;
		// If days is 0, return empty string; otherwise return full phrase with leading space.
		$this->placeholders['{days_until_deletion}'] = ( $days_until_deletion > 0 ) ? ' ' . $days_until_deletion : '';
		$this->placeholders['{shop_url}']            = home_url();

		if ( $this->is_enabled() && $this->get_recipient() ) {
			$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
		}

		$this->restore_locale();
	}

	/**
	 * Get content HTML
	 */
	public function get_content_html() {
		// If this is a preview and no user object is set, use current user for sample data.
		if ( ! $this->object ) {
			$current_user = wp_get_current_user();
			if ( $current_user && $current_user->ID ) {
				$this->object = $current_user;
				// Update placeholders with current user for preview.
				$this->placeholders['{customer_name}']  = $current_user->display_name;
				$this->placeholders['{customer_email}'] = $current_user->user_email;
			}
		}

		return wc_get_template_html(
			$this->template_html,
			array(
				'user'                => $this->object,
				'email_heading'       => $this->get_heading(),
				'additional_content'  => $this->format_string( $this->get_additional_content() ),
				'days_until_deletion' => $this->placeholders['{days_until_deletion}'],
				'shop_url'            => $this->placeholders['{shop_url}'],
				'customer_name'       => $this->placeholders['{customer_name}'],
				'sent_to_admin'       => false,
				'plain_text'          => false,
				'email'               => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Get content plain text
	 */
	public function get_content_plain() {
		// If this is a preview and no user object is set, use current user for sample data.
		if ( ! $this->object ) {
			$current_user = wp_get_current_user();
			if ( $current_user && $current_user->ID ) {
				$this->object = $current_user;
				// Update placeholders with current user for preview.
				$this->placeholders['{customer_name}']  = $current_user->display_name;
				$this->placeholders['{customer_email}'] = $current_user->user_email;
			}
		}

		return wc_get_template_html(
			$this->template_plain,
			array(
				'user'                => $this->object,
				'email_heading'       => $this->get_heading(),
				'additional_content'  => $this->format_string( $this->get_additional_content() ),
				'days_until_deletion' => $this->placeholders['{days_until_deletion}'],
				'shop_url'            => $this->placeholders['{shop_url}'],
				'customer_name'       => $this->placeholders['{customer_name}'],
				'sent_to_admin'       => false,
				'plain_text'          => true,
				'email'               => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Default content for email
	 */
	public function get_default_additional_content() {
		return __( "Dear {customer_name},\n\nThank you for registering with us! We noticed you haven't placed an order yet.\n\nTo keep your account active, please complete your first purchase within{days_until_deletion} days. Your account will be automatically maintained once you place an order.\n\nWe'd love to have you as a customer. Browse our products and place your first order today!\n\nIf you have any questions, feel free to reply to this email.\n\nThank you,\nAdmin", 'wuclean-database-cleaner-for-woocommerce' );
	}

	/**
	 * Get email recipient (for preview mode)
	 */
	public function get_default_recipient() {
		return get_option( 'admin_email' );
	}
}
