<?php
/**
 * Reward redemption email notification.
 *
 * @package Awesomemotive\Rewardswp\Notifications\Emails
 * @since 1.1.0
 */

namespace Awesomemotive\Rewardswp\Notifications\Emails;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractEmailNotification;
use function Awesomemotive\Rewardswp\Helpers\format_points_with_label;
use function Awesomemotive\Rewardswp\Helpers\get_panel_url;

/**
 * Email notification for reward redemption.
 *
 * @since 1.1.0
 */
class RewardRedemptionEmail extends AbstractEmailNotification {

	/**
	 * Get the notification name.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'reward_redemption';
	}

	/**
	 * Get the email subject.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function get_default_subject(): string {
		return __( 'Your reward is ready: {reward_title}', 'rewardswp' );
	}

	/**
	 * Get the HTML email template.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	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( 'Your reward is ready!', 'rewardswp' ); ?></h2>
					<p style="margin: 0 0 16px 0; color: #2c3e50; line-height: 1.6;"><?php esc_html_e( '{member_name}, you redeemed {points_redeemed_formatted} for {reward_title}. Use the coupon code below at checkout.', 'rewardswp' ); ?></p>
				</td>
			</tr>
		</table>

		<!-- Reward title -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 24px 0 0 0;">
			<tr>
				<td style="text-align: center; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Inter, Arial, sans-serif; font-size: 16px; line-height: 1.4; color: #2c3e50; font-weight: 700; padding: 0 0 16px 0;">
					{reward_title}
				</td>
			</tr>
		</table>

		<!-- Coupon code -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 0 0 24px 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;">
						{coupon_code}
					</div>
				</td>
			</tr>
		</table>

		<!-- Button -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 32px 0;">
			<tr>
				<td align="center">
					<a href="{shop_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( 'Start Shopping', 'rewardswp' ); ?>
						</span>
						<!--[if mso]>
						<i style="mso-font-width: -100%;">&nbsp;</i>
						<![endif]-->
					</a>
				</td>
			</tr>
		</table>

		<!-- Balance -->
		<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 0 0 16px 0;">
			<tr>
				<td style="text-align: center; padding: 8px 0;">
					<span style="font-size: 14px; color: #7f8c8d;"><?php esc_html_e( 'Remaining balance:', 'rewardswp' ); ?></span>
					<span style="font-size: 14px; font-weight: bold; color: #2c3e50;"> {current_balance_formatted}</span>
				</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.', 'rewardswp' ); ?>
						<a href="<?php echo esc_url( get_panel_url() ); ?>" style="color: {{brand_color}};"><?php esc_html_e( 'View your balance', 'rewardswp' ); ?></a>.
					</div>
				</td>
			</tr>
		</table>
		
		<?php
		return ob_get_clean();
	}

	/**
	 * Get the plain text email template.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function get_plain_text_template(): string {
		return __(
			'Your reward is ready!

{member_name}, you redeemed {points_redeemed_formatted} for {reward_title}. Use the coupon code below at checkout.

{reward_title}
Coupon: {coupon_code}

Remaining balance: {current_balance_formatted}

Start shopping: {shop_url}

Enjoy your reward.',
			'rewardswp'
		);
	}

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

	/**
	 * 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 members when they redeem a reward', 'rewardswp' );
	}

	/**
	 * 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'         => 'member_name',
				'label'       => __( 'Member First Name', 'rewardswp' ),
				'description' => __( 'First name of the member.', 'rewardswp' ),
				'example'     => 'John',
			],
			[
				'tag'         => 'member_full_name',
				'label'       => __( 'Member Full Name', 'rewardswp' ),
				'description' => __( 'Full name of the member.', 'rewardswp' ),
				'example'     => 'John Doe',
			],
			[
				'tag'         => 'reward_title',
				'label'       => __( 'Reward Title', 'rewardswp' ),
				'description' => __( 'Unified reward title (custom title if set, otherwise formatted amount).', 'rewardswp' ),
				'example'     => 'Free iPhone',
			],
			[
				'tag'         => 'coupon_code',
				'label'       => __( 'Coupon Code', 'rewardswp' ),
				'description' => __( 'The coupon code that was generated for this reward.', 'rewardswp' ),
				'example'     => 'alf-coins-abc123',
			],
			[
				'tag'         => 'points_redeemed_formatted',
				'label'       => __( 'Points Redeemed (Formatted)', 'rewardswp' ),
				'description' => __( 'Points redeemed with correct singular/plural label.', 'rewardswp' ),
				'example'     => format_points_with_label( 500 ),
			],
			[
				'tag'         => 'current_balance_formatted',
				'label'       => __( 'Current Balance (Formatted)', 'rewardswp' ),
				'description' => __( 'Member\'s remaining points balance with correct singular/plural label.', 'rewardswp' ),
				'example'     => format_points_with_label( 250 ),
			],
			[
				'tag'         => 'site_url',
				'label'       => __( 'Site URL', 'rewardswp' ),
				'description' => __( 'Link to your store homepage.', 'rewardswp' ),
				'example'     => home_url(),
			],
		];
	}
}
