<?php
/**
 * File: Ecre_Email.php
 *
 * The Ecre_Email class extends the WC_Email class to handle referral email notifications.
 *
 * @package WPPOOL\ECRE
 * @since   1.0.0
 */

namespace ECRE\Admin;

if ( ! class_exists( 'Ecre_Email' ) ) {
	/**
	 * Class Ecre_Email
	 *
	 * Represents an email sent to the referrer when their referral coupon is used.
	 *
	 * @since 1.0.0
	 */
	class Ecre_Email extends \WC_Email {
		/**
		 * Indicates whether the user has a professional (pro) account.
		 *
		 * @var bool
		 */
		public $is_pro;

		/**
		 * Ecre_Email constructor.
		 *
		 * Initializes the email settings, including subject, heading, and description,
		 * and sets up the action hooks necessary for sending referral-related emails.
		 *
		 * @param bool $is_pro Indicates whether the user has a professional (pro) account.
		 *                     If true, additional features related to emails might be enabled.
		 *
		 * @since 1.0.0
		 */
		public function __construct( $is_pro ) {
			$this->is_pro      = $is_pro;
			$this->id          = 'ecre_referral_email';
			$this->subject     = '';
			$this->heading     = '';
			$this->description = '';

			// Initialize the parent class.
			parent::__construct();
		}


		/**
		 * Triggers the sending of a referral or reward email based on the specified parameters.
		 *
		 * This method sends a custom email to either a referral recipient or a user receiving a reward,
		 * depending on the type of email specified ('referral_email' or 'reward_email').
		 *
		 * @param int    $referral_user_id The ID of the referring user. Used to retrieve sender's details for reward emails.
		 * @param string $coupon_code      The coupon code to include in the email, if applicable.
		 * @param string $recipient_name   Optional. The name of the email recipient. Defaults to an empty string.
		 * @param string $recipient_email  Optional. The recipient's email address. Defaults to an empty string.
		 * @param string $email_type       Optional. Specifies the type of email to send: 'referral_email' or 'reward_email'.
		 *                                 If left empty, no email will be triggered.
		 * @param string $discount         Optional. The discount amount or description to include in the email. Defaults to an empty string.
		 *
		 * @since 1.0.0
		 * @return bool True if the email was successfully sent, false otherwise.
		 */
		public function trigger( $referral_user_id, $coupon_code, $recipient_name = '', $recipient_email = '', $email_type = '', $discount = '' ) {
			$default_settings = ecre_default_settings();
			$success          = false;
			// Retrieve settings from the database.
			$settings           = get_option( 'ecre_settings', array() );
			$ecre_currency_sign = ecre_custom_currency_symbol();
			$this->object       = get_userdata( $referral_user_id );
			$user               = $this->object->display_name;

			if ( 'referral_email' === $email_type ) {
				$this->recipient   = $recipient_email;
				$recipient_name    = $recipient_name;
				$this->subject     = $this->is_pro ? $settings['referralEmailSubject'] : $default_settings['referralEmailSubject'];
				$this->heading     = $this->is_pro ? $settings['referralEmailHeading'] : $default_settings['referralEmailHeading'];
				$this->description = $this->is_pro ? $settings['referralEmailBody'] : $default_settings['referralEmailBody'];

				// Generate the HTML email content using the template class.
				$html_email = \ECRE\Templates\Ecre_Email_Template::ecre_coupon_html_email_template( $this->subject, $this->heading, $this->description, $recipient_name, $coupon_code, $discount, $email_type, $settings, $default_settings, $this->is_pro, $user );

			} else {
				$this->recipient       = $this->object->user_email;
				$this->subject         = $this->is_pro ? $settings['rewardEmailSubject'] : $default_settings['rewardEmailSubject'];
				$this->heading         = $this->is_pro ? $settings['rewardEmailHeading'] : $default_settings['rewardEmailHeading'];
				$this->description     = $this->is_pro ? $settings['rewardEmailBody'] : $default_settings['rewardEmailBody'];
				$reward_discount       = $this->is_pro ? $settings['rewardDiscount'] : $default_settings['rewardDiscount'];
				$reward_point_discount = $this->is_pro ? $settings['redeemDiscount'] : $default_settings['redeemDiscount'];
				$redeem_point          = $this->is_pro ? $settings['redeemPoint'] : $default_settings['redeemPoint'];
				$settings_reward_type  = $settings['rewardType']['value'];
				$reward_discount_type  = '';

				if ( ! $this->is_pro && ( 'percent' !== $settings_reward_type && 'fixed' !== $settings_reward_type ) ) {
					$reward_discount_type = $default_settings['rewardType']['value'];
				} else {
					$reward_discount_type = $settings_reward_type;
				}

				if ( 'percent' === $reward_discount_type ) {
					$reward_discount = $reward_discount . '%';
				} else {
					$reward_discount = $reward_discount . $ecre_currency_sign;
				}

				if ( 'reward_point' === $reward_discount_type ) {
					$reward_point_discount = $reward_point_discount . $ecre_currency_sign;
					$html_email            = \ECRE\Templates\Ecre_Email_Template::ecre_point_html_email_template( $this->subject, $this->heading, $this->description, $user, $redeem_point, $reward_point_discount, $email_type, $settings );
				} else {
					$html_email = \ECRE\Templates\Ecre_Email_Template::ecre_coupon_html_email_template( $this->subject, $this->heading, $this->description, $user, $coupon_code, $reward_discount, $email_type, $settings, $default_settings, $this->is_pro );
				}
			}

			if ( $this->is_enabled() && $this->get_recipient() ) {
				$mailer = WC()->mailer();

				// Send the email with subject, HTML content, headers, and attachments.
				$sent = $mailer->send( $this->get_recipient(), $this->subject, $html_email, $this->get_headers(), $this->get_attachments() );

				if ( $sent ) {
					$success = true;
				}
			}

			return $success;
		}
	}
}
