<?php
/**
 * File: Ecre_Email_Template.php
 *
 * The Ecre_Email_Template class handles the HTML email template for referral notifications.
 *
 * @package  ECRE\Templates
 * @since   1.0.0
 */

namespace ECRE\Templates;

if ( ! class_exists( 'Ecre_Email_Template' ) ) {
	/**
	 * Class Ecre_Email_Template
	 *
	 * Represents the HTML email template for referral notifications.
	 *
	 * @since 1.0.0
	 */
	class Ecre_Email_Template {
		/**
		 * Get the HTML email template with placeholders replaced.
		 *
		 * Generates a referral email template by replacing dynamic placeholders with actual content.
		 * This includes user information, coupon details, discount value, and customized messaging
		 * based on plugin settings and account type.
		 *
		 * @param string $subject          The email subject.
		 * @param string $heading          The email heading.
		 * @param string $description      The additional email content/body.
		 * @param string $user             The recipient's username.
		 * @param string $coupon_code      The referral coupon code.
		 * @param string $discount         The discount amount or percentage.
		 * @param string $email_type       The type of email (e.g., 'referrer', 'referred').
		 * @param array  $settings         The current plugin settings.
		 * @param array  $default_settings The fallback default settings.
		 * @param bool   $is_pro           Whether the user has a professional (pro) account.
		 * @param string $referrer_name    Optional. The name of the referrer (for personalized content). Default ''.
		 *
		 * @return string The HTML email template with placeholders replaced.
		 *
		 * @since 1.0.0
		 */
		public static function ecre_coupon_html_email_template( $subject, $heading, $description, $user, $coupon_code, $discount, $email_type, $settings, $default_settings, $is_pro, $referrer_name = '' ) {
			// Define the HTML email structure with placeholders.
			$html_email = '
			<!DOCTYPE html>
			<html lang="en">
				<head>
					<meta charset="UTF-8">
					<title>{subject}</title>
					<style>
						/* Reset styles to ensure consistent rendering */
						body,
						table,
						td,
						a {
							-ms-text-size-adjust: 100%;
							-webkit-text-size-adjust: 100%;
						}
				
						table {
							border-collapse: collapse;
							mso-table-lspace: 0pt;
							mso-table-rspace: 0pt;
						}
				
				
						td {
							vertical-align: top;
							padding: 0;
						}
				
						/* Email styles */
						body {
							background-color: #f5f5f5;
							font-family: Arial, sans-serif;
							padding: 20px;
						}
				
						table {
							background-color: #ffffff;
							border: 1px solid #d8e2e5;
							border-radius: 3px;
							box-shadow: 0 2px 3px rgba(0, 0, 0, 0.04);
							width: 100%;
							max-width: 600px;
							margin: 0 auto;
						}
				
						.ecre-email-header h1 {
							color: #FFFFFF;
							font-size: 24px;
							line-height: 1.2;
							font-weight: 500;
							padding-right: 30px
						}

						.ecre-email-header a {
							text-decoration: none;
						}
				
						.ecre-email-content{
							padding: 15px 30px;
						}
				
						.ecre-email-content p {
							color: #5F666B;
							font-size: 16px;
						}
				
						.ecre-email-content strong {
							font-weight: bold;
						}
				
						.ecre-email-content .coupon-code {
							font-size: 20px;
							text-transform: uppercase;
							text-align: center;
							font-weight: 700;
							color:  #394147;
							margin: 30px 0;
						}
				
						.ecre-email-content .coupon-discount {
							font-size: 18px;
							color:#5F666B;
							text-align: center;
						}
				
						.ecre-email-content .coupon-discount span {    
							color:#F27139;
						}
				
						.ecre-email-header{
							background: #7956AD;
							padding: 15px 30px;
							color: #FFFFFF;

						}
						
						.recepent-name h4{
							color: #5F666B;
						}
					</style>
				</head>
			
				<body>
					<table>
						<tr>
							<td>
								<div class="ecre-email-header">
									<h1>{email_heading}</h1>
								</div>
								<div class="ecre-email-content">
									<div class="recepent-name">
										<h4>{greeting} {user},</h4>
									</div>
									<div>{additional_content}</div>
									<p class="coupon-code">{coupon_code}</p>
									 {referral_link}
									<p class="coupon-discount">{enjoy} <span> {coupon_discount} {discount}</span> {with_this_coupon}</p>
								</div>
							</td>
						</tr>
					</table>
				</body>
			
			</html>';

			// Conditional referral note.
			$referral_note         = '';
			$referral_link         = '';
			$with_this_coupon_text = __( 'with this coupon code', 'echo-rewards' );

			// Handle referralCouponType for referral_email.

			$settings_discount_type = $settings['referralCouponType']['value'];
			$referral_discount_type = '';

			if ( ! $is_pro && ( 'percent' !== $settings_discount_type && 'fixed' !== $settings_discount_type ) ) {
				$referral_discount_type = $default_settings['referralCouponType']['value'];
			} else {
				$referral_discount_type = $settings_discount_type;
			}

			if ( ( 'sign_up_fee' === (string) $referral_discount_type || 'sign_up_fee_percent' === (string) $referral_discount_type )
				&& 'referral_email' === (string) $email_type
			) {
				$with_this_coupon_text = $settings['enableReferralLink']
					? __( 'on subscriptions Sign Up Fee with this coupon code or link', 'echo-rewards' )
					: __( 'on subscriptions Sign Up Fee with this coupon code', 'echo-rewards' );
			} elseif (
				( 'recurring_fee' === (string) $referral_discount_type || 'recurring_percent' === (string) $referral_discount_type )
				&& 'referral_email' === (string) $email_type
			) {
				$with_this_coupon_text = $settings['enableReferralLink']
					? __( 'on subscription renewals with this coupon code or link', 'echo-rewards' )
					: __( 'on subscription renewals with this coupon code', 'echo-rewards' );
			} elseif (
				( 'percent' === (string) $referral_discount_type || 'fixed' === (string) $referral_discount_type )
				&& 'referral_email' === (string) $email_type
			) {
				$with_this_coupon_text = $settings['enableReferralLink']
					? __( 'with this coupon code or link', 'echo-rewards' )
					: __( 'with this coupon code', 'echo-rewards' );
			}

			// Handle rewardType for non-referral_email.
			if (
				( 'sign_up_fee' === (string) $settings['rewardType']['value'] || 'sign_up_fee_percent' === (string) $settings['rewardType']['value'] )
				&& 'referral_email' !== $email_type
			) {
				$with_this_coupon_text = __( 'on subscriptions Sign Up Fee with this coupon code', 'echo-rewards' );
			} elseif (
				( 'recurring_fee' === (string) $settings['rewardType']['value'] || 'recurring_percent' === (string) $settings['rewardType']['value'] )
				&& 'referral_email' !== (string) $email_type
			) {
				$with_this_coupon_text = __( 'on subscription renewals with this coupon code', 'echo-rewards' );
			} elseif (
				( 'percent' === (string) $settings['rewardType']['value'] || 'fixed' === (string) $settings['rewardType']['value'] )
				&& 'referral_email' !== (string) $email_type
			) {
				$with_this_coupon_text = __( 'with this coupon code', 'echo-rewards' );
			}

			if ( 'referral_email' === (string) $email_type ) {
				$site_url = get_site_url();
				$ref_link = esc_url( $site_url . '/?ref=' . strtolower( $coupon_code ) );

				if ( $settings['enableReferralLink'] ) {
					$referral_link = sprintf(
						'<p><strong>Referral Link:</strong> <a href="%s">%s</a></p>',
						$ref_link,
						$ref_link
					);
				}
			}

			// Replace placeholders with dynamic values.
			$description = str_replace( '%referrer_name', $referrer_name, $description );
			$description = str_replace( '%referral_discount', $discount, $description );
			$description = str_replace( '%reward_discount', $discount, $description );
			$description = str_replace( '%referral_coupon', $coupon_code, $description );
			$description = str_replace( '%reward_coupon', $coupon_code, $description );
			$description = str_replace( '%site_name', get_bloginfo( 'name' ), $description );

			// Replace placeholders in the email template.
			$html_email = str_replace( '{subject}', $subject, $html_email );
			$html_email = str_replace( '{email_heading}', $heading, $html_email );
			$html_email = str_replace( '{additional_content}', $description, $html_email );
			$html_email = str_replace( '{user}', $user, $html_email );
			$html_email = str_replace( '{coupon_code}', $coupon_code, $html_email );
			$html_email = str_replace( '{coupon_discount}', $discount, $html_email );
			$html_email = str_replace( '{greeting}', __( 'Hi', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{enjoy}', __( 'Enjoy', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{discount}', __( 'discount', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{with_this_coupon}', $with_this_coupon_text, $html_email );
			$html_email = str_replace( '{referral_link}', $referral_link, $html_email );

			return $html_email;
		}

		/**
		 * Generates the HTML content for a reward point email template.
		 *
		 * This method creates an HTML email structure with placeholders that are dynamically
		 * replaced with provided values such as the subject, heading, user name, and discount details.
		 * The email is styled and structured to display information about earned reward points.
		 *
		 * @param string $subject     The subject line of the email.
		 * @param string $heading     The heading text of the email.
		 * @param string $description Additional content or description to be included in the email.
		 * @param string $user        The recipient's name to be displayed in the email.
		 * @param string $point       The number of points the user has earned or is being rewarded.
		 * @param string $discount    The discount amount or value associated with the reward points.
		 *
		 * @return string The generated HTML email content with all placeholders replaced.
		 */
		public static function ecre_point_html_email_template( $subject, $heading, $description, $user, $point, $discount ) {
			// Define the HTML email structure with placeholders.
			$html_email = '
			<!DOCTYPE html>
			<html lang="en">
				<head>
					<meta charset="UTF-8">
					<title>{subject}</title>
					<style>
						/* Reset styles to ensure consistent rendering */
						body,
						table,
						td,
						a {
							-ms-text-size-adjust: 100%;
							-webkit-text-size-adjust: 100%;
						}
				
						table {
							border-collapse: collapse;
							mso-table-lspace: 0pt;
							mso-table-rspace: 0pt;
						}
				
				
						td {
							vertical-align: top;
							padding: 0;
						}
				
						/* Email styles */
						body {
							background-color: #f5f5f5;
							font-family: Arial, sans-serif;
							padding: 20px;
						}
				
						table {
							background-color: #ffffff;
							border: 1px solid #d8e2e5;
							border-radius: 3px;
							box-shadow: 0 2px 3px rgba(0, 0, 0, 0.04);
							width: 100%;
							max-width: 600px;
							margin: 0 auto;
						}
				
						.ecre-email-header h1 {
							color: #FFFFFF;
							font-size: 24px;
							line-height: 1.2;
							font-weight: 500;
							padding-right: 30px
						}

						.ecre-email-header a {
							text-decoration: none;
						}
				
						.ecre-email-content{
							padding: 15px 30px;
						}
				
						.ecre-email-content p {
							color: #5F666B;
							font-size: 16px;
						}
				
						.ecre-email-content strong {
							font-weight: bold;
						}
				
						.ecre-email-content .point {
							font-size: 20px;
							text-transform: uppercase;
							text-align: center;
							font-weight: 700;
							color:  #394147;
							margin: 30px 0;
						}
				
						.ecre-email-content .coupon-discount {
							font-size: 18px;
							color:#5F666B;
							text-align: center;
						}
				
						.ecre-email-content .coupon-discount span {    
							color:#F27139;
						}
				
						.ecre-email-header{
							background: #7956AD;
							padding: 15px 30px;
							color: #FFFFFF;

						}
						
						.recepent-name h4{
							color: #5F666B;
						}
					</style>
				</head>
			
				<body>
					<table>
						<tr>
							<td>
								<div class="ecre-email-header">
									<h1>{email_heading}</h1>
								</div>
								<div class="ecre-email-content">
									<div class="recepent-name">
										<h4>{greeting} {user},</h4>
									</div>
									<p>{additional_content}</p>
									<p class="point"> {rewards_point} {dis_point} {re_points}</p>
									<p class="coupon-discount">{get} <span> {point_discount} {off_txt} </span> {for_each}    {dis_point} {reward_points}</p>
								</div>
							</td>
						</tr>
					</table>
				</body>
			
			</html>';

			// Replace placeholders with email content.
			$html_email = str_replace( '{subject}', $subject, $html_email );
			$html_email = str_replace( '{email_heading}', $heading, $html_email );
			$html_email = str_replace( '{additional_content}', $description, $html_email );
			$html_email = str_replace( '{user}', $user, $html_email );
			$html_email = str_replace( '{dis_point}', $point, $html_email );
			$html_email = str_replace( '{point_discount}', $discount, $html_email );
			$html_email = str_replace( '{greeting}', __( 'Hi', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{get}', __( 'Get', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{rewards_point}', __( 'Reward Point:', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{re_points}', __( 'Points', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{discount}', __( 'discount', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{off_txt}', __( 'off', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{for_each}', __( 'for each', 'echo-rewards' ), $html_email );
			$html_email = str_replace( '{reward_points}', __( 'reward points', 'echo-rewards' ), $html_email );

			return $html_email;
		}
	}
}
