<?php
/**
 * Friend reward email notification (Lite & Pro).
 *
 * @package Awesomemotive\Rewardswp\Notifications\Emails
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Notifications\Emails;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractEmailNotification;
use function Awesomemotive\Rewardswp\Helpers\can_access_rewardswp_feature;

/**
 * Class FriendRewardEmail
 *
 * Handles the email notification sent to a friend when they claim their reward.
 *
 * @since 1.0.0
 */
class FriendRewardEmail extends AbstractEmailNotification {
	/**
	 * Get the unique email name.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'friend_reward_email';
	}

	/**
	 * Get the display title for the email.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	public function get_title(): string {
		return __( 'Friend Reward', 'rewardswp' );
	}

	/**
	 * Retrieve the default email subject.
	 *
	 * @since 1.1.0
	 *
	 * @return string The default email subject.
	 */
	protected function get_default_subject(): string {
		return __( 'Your {reward_amount} reward confirmation', 'rewardswp' );
	}

	/**
	 * Retrieve the plain text email template.
	 *
	 * @since 1.0.0
	 *
	 * @return string The plain text email template.
	 */
	public function get_plain_text_template(): string {
		$output  = esc_html__( "You've got a reward!", 'rewardswp' ) . PHP_EOL . PHP_EOL;
		$output .= esc_html__( "Someone special referred you, and you've earned a discount on your first order.", 'rewardswp' ) . PHP_EOL . PHP_EOL;
		$output .= esc_html__( 'Your reward', 'rewardswp' ) . PHP_EOL;
		$output .= esc_html__( 'Coupon: {reward_coupon}', 'rewardswp' ) . PHP_EOL;
		$output .= esc_html__( 'Valid until {reward_expiration_time}', 'rewardswp' ) . PHP_EOL . PHP_EOL;
		$output .= sprintf(
			/* translators: %s is the site's URL */
			esc_html__( 'Shop with your reward: %s', 'rewardswp' ),
			esc_url( home_url() )
		) . PHP_EOL;

		return $output;
	}

	/**
	 * Retrieve the default HTML email template.
	 *
	 * @since 1.1.0
	 *
	 * @return string The default HTML email template.
	 */
	protected function get_default_html_template(): string {
		ob_start();

		?>

		<!-- Header -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 0 0 16px 0;">
			<tr>
				<td style="text-align: center;">
					<h2 style="margin: 0 0 16px 0; font-size: 28px; font-weight: bold; line-height: 1.3; color: #2c3e50;"><?php esc_html_e( "You've got a reward!", 'rewardswp' ); ?></h2>
					<p style="margin: 0 0 16px 0; color: #2c3e50; line-height: 1.6;"><?php esc_html_e( "Someone special referred you, and you've earned a discount on your first order.", 'rewardswp' ); ?></p>
				</td>
			</tr>
		</table>

		<!-- Reward details -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 24px 0 0 0;">
			<tr>
				<td style="text-align: center; padding: 0 0 16px 0;">
					<p style="margin: 0; color: #2c3e50; line-height: 1.6;"><?php esc_html_e( "You've earned a {reward_amount} coupon", 'rewardswp' ); ?></p>
				</td>
			</tr>
		</table>

		<!-- Coupon code -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 0 0 0 0;">
			<tr>
				<td>
					<div style="border: 1px solid #d1d5db; border-radius: 8px; padding: 16px 20px; text-align: center; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; font-size: 18px; font-weight: bold; color: #2c3e50; letter-spacing: 1px;">
						{reward_coupon}
					</div>
				</td>
			</tr>
		</table>

		<!-- Expiration notice -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 0 0 24px 0;">
			<tr>
				<td style="text-align: center; padding: 12px 0 0 0; font-size: 14px; color: #7f8c8d; line-height: 1.4;">
					<?php esc_html_e( 'Valid until {reward_expiration_time}', 'rewardswp' ); ?>
				</td>
			</tr>
		</table>

		<!-- Button table -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 32px 0;">
			<tr>
				<td align="center">
					<a href="<?php echo esc_url( home_url() ); ?>" style="background-color: {{brand_color}} !important; color: {{brand_text_color}} !important; text-decoration: none !important; font-weight: bold !important; font-size: 16px !important; display: inline-block !important; padding: 14px 28px !important; line-height: 1.4 !important; border-radius: 8px !important; mso-padding-alt: 0; text-underline-color: {{brand_color}};">
						<!--[if mso]>
						<i style="mso-font-width: -100%; mso-text-raise: 30pt;">&nbsp;</i>
						<![endif]-->
						<span style="mso-text-raise: 15pt;">
							<?php esc_html_e( 'Shop with your reward', 'rewardswp' ); ?>
						</span>
						<!--[if mso]>
						<i style="mso-font-width: -100%;">&nbsp;</i>
						<![endif]-->
					</a>
				</td>
			</tr>
		</table>

		<!-- Footer message -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 24px 0 0 0;">
			<tr>
				<td style="text-align: center;">
					<div style="font-size: 14px; color: #7f8c8d; line-height: 1.6;">
						<?php esc_html_e( 'Enjoy your reward and happy shopping.', 'rewardswp' ); ?>
						<a href="<?php echo esc_url( home_url() ); ?>" style="color: {{brand_color}};"><?php esc_html_e( 'See what you can earn', 'rewardswp' ); ?></a>.
					</div>
				</td>
			</tr>
		</table>

		<?php

		return ob_get_clean();
	}

	/**
	 * Get the email context.
	 *
	 * @since 1.1.0
	 *
	 * @return string The email context.
	 */
	public function get_context(): string {
		return 'referral';
	}

	/**
	 * Get the recipient type.
	 *
	 * @since 1.1.0
	 *
	 * @return string The recipient type.
	 */
	public function get_recipient_type(): string {
		return 'member';
	}

	/**
	 * Get the email description.
	 *
	 * @since 1.1.0
	 *
	 * @return string The email description.
	 */
	public function get_description(): string {
		return __( 'Sent to referred friends when they claim their referral reward', 'rewardswp' );
	}

	/**
	 * Determine if the email can be accessed with the current license.
	 *
	 * @since 1.1.0
	 *
	 * @return bool
	 */
	public function has_access(): bool {
		return can_access_rewardswp_feature( 'friend_reward_email' );
	}

	/**
	 * Retrieve the feature slug associated with this email.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	public function get_feature_slug(): string {
		return 'friend_reward_email';
	}

	/**
	 * Get merge tag definitions for admin UI.
	 *
	 * @since 1.1.0
	 *
	 * @return array Array of merge tag definitions.
	 */
	public function get_merge_tag_definitions(): array {
		return [
			[
				'tag'         => 'reward_amount',
				'label'       => __( 'Reward Amount', 'rewardswp' ),
				'description' => __( "Friend's reward value or product name.", 'rewardswp' ),
				'example'     => \Awesomemotive\Rewardswp\Helpers\format_currency( 10, '', false ),
			],
			[
				'tag'         => 'reward_coupon',
				'label'       => __( 'Reward Coupon', 'rewardswp' ),
				'description' => __( 'Coupon code to redeem the reward.', 'rewardswp' ),
				'example'     => 'points-a1b2c3d4',
			],
			[
				'tag'         => 'reward_expiration_time',
				'label'       => __( 'Reward Expiration', 'rewardswp' ),
				'description' => __( 'When the reward expires.', 'rewardswp' ),
				'example'     => __( '30 days', 'rewardswp' ),
			],
		];
	}
}
