<?php
/**
 * Points expiration email notification.
 *
 * @package Awesomemotive\Rewardswp\Notifications\Emails
 * @since 1.0.6
 */

namespace Awesomemotive\Rewardswp\Notifications\Emails;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractEmailNotification;
use function Awesomemotive\Rewardswp\Helpers\can_access_rewardswp_feature;
use function Awesomemotive\Rewardswp\Helpers\get_panel_url;
use function Awesomemotive\Rewardswp\Helpers\get_points_currency_plural;
use function Awesomemotive\Rewardswp\Helpers\get_points_currency_singular;

/**
 * Class PointsExpirationEmail
 *
 * Handles the email notification sent to members when their points are about to expire.
 *
 * @since 1.0.0
 */
class PointsExpirationEmail extends AbstractEmailNotification {
	/**
	 * Get the unique email name.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'points_expiration_email';
	}

	/**
	 * Get the display title for the email.
	 *
	 * @since 1.1.1
	 *
	 * @return string
	 */
	public function get_title(): string {
		return __( 'Points Expiry Email', 'rewardswp' );
	}

	/**
	 * Retrieve the default email subject.
	 *
	 * @since 1.0.0
	 * @return string The default email subject.
	 */
	protected function get_default_subject(): string {
		return __( 'Your {currency_plural} are expiring soon!', '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__( 'Your {currency_plural} are expiring soon!', 'rewardswp' ) . PHP_EOL . PHP_EOL;
		$output .= esc_html__( '{member_name}, you have {expiring_points_count} {currency_plural} that will expire in {days_before} days. Redeem them before {expiration_date}.', 'rewardswp' ) . PHP_EOL . PHP_EOL;
		$output .= esc_html__( 'Balance: {current_points_balance} {currency_plural}', 'rewardswp' ) . PHP_EOL . PHP_EOL;
		$output .= sprintf(
			/* translators: %s is the URL to redeem rewards */
			esc_html__( 'Redeem your {currency_plural}: %s', 'rewardswp' ),
			esc_url( get_panel_url( 'redeem' ) )
		) . PHP_EOL . PHP_EOL;
		$output .= esc_html__( 'Use them or lose them.', 'rewardswp' );

		return $output;
	}

	/**
	 * Retrieve the default HTML email template.
	 *
	 * @since 1.0.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( 'Your {currency_plural} are expiring soon!', 'rewardswp' ); ?></h2>
					<p style="margin: 0 0 16px 0; color: #2c3e50; line-height: 1.6;"><?php esc_html_e( '{member_name}, you have {expiring_points_count} {currency_plural} that will expire in {days_before} days. Redeem them before {expiration_date}.', 'rewardswp' ); ?></p>
				</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="<?php echo esc_url( get_panel_url( 'redeem' ) ); ?>" 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( 'Redeem Your {currency_plural}', '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( 'Balance:', 'rewardswp' ); ?></span>
					<span style="font-size: 14px; font-weight: bold; color: #2c3e50;"> {current_points_balance} {currency_plural}</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( 'Use them or lose them.', '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 email context.
	 *
	 * @since 1.0.0
	 *
	 * @return string The email context.
	 */
	public function get_context(): string {
		return 'points';
	}

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

	/**
	 * Get the email description.
	 *
	 * @since 1.0.0
	 *
	 * @return string The email description.
	 */
	public function get_description(): string {
		return esc_html__( 'Sent to members when their points are about to expire', '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( 'points_expiry_email' );
	}

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

	/**
	 * Get merge tag definitions for admin UI.
	 *
	 * @since 1.0.0
	 *
	 * @return array Array of merge tag definitions.
	 */
	public function get_merge_tag_definitions(): array {
		return [
			[
				'tag'         => 'member_name',
				'label'       => esc_html__( 'Member First Name', 'rewardswp' ),
				'description' => esc_html__( 'First name of the member.', 'rewardswp' ),
				'example'     => 'John',
			],
			[
				'tag'         => 'member_full_name',
				'label'       => esc_html__( 'Member Full Name', 'rewardswp' ),
				'description' => esc_html__( 'Full name of the member.', 'rewardswp' ),
				'example'     => 'John Doe',
			],
			[
				'tag'         => 'expiring_points_count',
				'label'       => esc_html__( 'Expiring Points Count', 'rewardswp' ),
				'description' => esc_html__( 'Number of points that will expire.', 'rewardswp' ),
				'example'     => '500',
			],
			[
				'tag'         => 'current_points_balance',
				'label'       => esc_html__( 'Current Points Balance', 'rewardswp' ),
				'description' => esc_html__( 'Member\'s total current points balance.', 'rewardswp' ),
				'example'     => '1250',
			],
			[
				'tag'         => 'days_before',
				'label'       => esc_html__( 'Days Until Expiration', 'rewardswp' ),
				'description' => esc_html__( 'Number of days until the points expire.', 'rewardswp' ),
				'example'     => '7',
			],
			[
				'tag'         => 'expiration_date',
				'label'       => esc_html__( 'Expiration Date', 'rewardswp' ),
				'description' => esc_html__( 'Formatted date when the points will expire.', 'rewardswp' ),
				'example'     => esc_html__( 'December 15, 2024', 'rewardswp' ),
			],
			[
				'tag'         => 'currency_singular',
				'label'       => esc_html__( 'Currency Singular', 'rewardswp' ),
				'description' => esc_html__( 'Points currency in singular form.', 'rewardswp' ),
				'example'     => get_points_currency_singular(),
			],
			[
				'tag'         => 'currency_plural',
				'label'       => esc_html__( 'Currency Plural', 'rewardswp' ),
				'description' => esc_html__( 'Points currency in plural form.', 'rewardswp' ),
				'example'     => get_points_currency_plural(),
			],
		];
	}

	/**
	 * Get the placeholders that are supported by this email.
	 *
	 * @since 1.0.0
	 * @deprecated Use get_merge_tag_definitions() instead
	 *
	 * @return array The placeholders.
	 */
	public function get_placeholders(): array {
		return [
			'{member_name}'            => esc_html__( 'Member first name', 'rewardswp' ),
			'{member_full_name}'       => esc_html__( 'Member full name', 'rewardswp' ),
			'{expiring_points_count}'  => esc_html__( 'Number of points expiring', 'rewardswp' ),
			'{current_points_balance}' => esc_html__( 'Current total points balance', 'rewardswp' ),
			'{days_before}'            => esc_html__( 'Number of days until expiration', 'rewardswp' ),
			'{expiration_date}'        => esc_html__( 'Formatted expiration date', 'rewardswp' ),
			'{currency_singular}'      => esc_html__( 'Points currency singular form', 'rewardswp' ),
			'{currency_plural}'        => esc_html__( 'Points currency plural form', 'rewardswp' ),
		];
	}

	/**
	 * Returns the default email heading.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	protected function get_default_heading(): string {
		return esc_html__( 'Your {currency_plural} are expiring soon', 'rewardswp' );
	}

	/**
	 * Get the email priority level for this notification type.
	 *
	 * @since 1.0.0
	 *
	 * @return string 'high', 'normal', or 'low'
	 */
	public function get_priority(): string {
		return 'high'; // High priority for expiration notifications
	}
}
