<?php
/**
 * Email Messages Composer for WC Affiliate Plugin.
 *
 * Handles email content composition and template rendering for various
 * affiliate-related email types. Works with the Mailer class for sending.
 *
 * @since   3.0.0
 * @package WC_Affiliate
 */

namespace WC_Affiliate\Helpers\Email;

use WC_Affiliate\Models\Affiliate;
use WC_Affiliate\Models\Referral;
use WC_Affiliate\Models\Transaction;

defined( 'ABSPATH' ) || exit;

/**
 * Messages class for composing email content.
 */
class Messages {

	/**
	 * Compose affiliate applied email for affiliate.
	 *
	 * @param int   $user_id        Affiliate user ID.
	 * @param array $affiliate_data Affiliate data array.
	 *
	 * @return array Email content data.
	 */
	public function compose_affiliate_applied_affiliate( int $user_id, array $affiliate_data ): array {
		$enable = apply_filters( 'wc_affiliate_email_affiliate_application_enabled', wca_is_affiliate_application_email_enabled(), $user_id );
		// Send welcome email if enabled.
		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		$enable_email_verify = apply_filters( 'wc_affiliate_email_verification_enabled', wca_is_email_validation_enabled(), $user_id );
		$email               = $affiliate_data['user_email'];
		$first_name          = $affiliate_data['first_name'];
		$site_name           = get_bloginfo( 'name' );
		$dashboard_url       = wca_get_dashboard();

		$affiliate = new Affiliate( $user_id );

		// Allow filtering recipients for affiliate application email.
		$recipients = apply_filters( 'wc_affiliate_email_affiliate_application_recipients', array( $email ), $user_id, $affiliate_data );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name'] = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );

		// Handle email verification if enabled.
		if ( $enable_email_verify ) {
			$validation_data = wp_json_encode(
				array(
					'id'    => $user_id,
					'email' => $email,
					'time'  => current_time( 'mysql' ),
				)
			);
			$validation_data = wca_ncrypt()->encrypt( $validation_data );
			$verify_url      = add_query_arg( 'validate', $validation_data, $dashboard_url );

			// Prepare highlight content.
			$highlight_content  = '<h3 style="margin-top: 0; color: #92400e;">📧 Email Verification Required</h3>';
			$highlight_content .= '<p>Please verify your email address to activate your affiliate account and start earning commissions.</p>';

			// Generate content sections.
			$greeting  = apply_filters( 'wc_affiliate_email_greeting_content', '', $first_name );
			$highlight = apply_filters( 'wc_affiliate_email_highlight_content', '', $highlight_content );
			$button    = apply_filters( 'wc_affiliate_email_button_content', '', $dashboard_url, 'Visit My Dashboard' );
			$signature = apply_filters( 'wc_affiliate_email_signature_content', '', $site_name );

			// Allow filtering verification email content.
			$content_data = apply_filters(
				'wc_affiliate_email_verification_content_data',
				array(
					'greeting'      => $greeting,
					'highlight'     => $highlight,
					'verify_url'    => $verify_url,
					'dashboard_url' => $dashboard_url,
					'button'        => $button,
					'signature'     => $signature,
				),
				$user_id,
				$affiliate_data
			);

			return array(
				'enabled'    => true,
				'recipients' => $recipients,
				'subject'    => apply_filters( 'wc_affiliate_email_verification_subject', \__( 'Verify Your Email - Welcome to Our Affiliate Program', 'wc-affiliate' ), $user_id, $affiliate_data ),
				'content'    => wca_get_template( 'messages/affiliate-verification', 'views/emails', $content_data ),
				'context'    => $context_defaults,
			);
		}

		$subject = apply_filters( 'wc_affiliate_email_affiliate_application_subject', wca_get_affiliate_application_subject(), $user_id, $affiliate_data );
		$message = apply_filters( 'wc_affiliate_email_affiliate_application_message', wca_get_affiliate_application_message(), $user_id, $affiliate_data );

		return array(
			'enabled'    => true,
			'recipients' => $recipients,
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $context_defaults,
		);
	}

	/**
	 * Compose affiliate applied email for admin.
	 *
	 * @param int   $user_id        User ID.
	 * @param array $affiliate_data Affiliate data array.
	 *
	 * @return array Email content data.
	 */
	public function compose_affiliate_applied_admin( int $user_id, array $affiliate_data ): array {
		$enable = apply_filters( 'wc_affiliate_email_admin_application_enabled', wca_is_admin_application_email_enabled(), $user_id );
		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		$email    = apply_filters( 'wc_affiliate_email_admin_application_recipients', wca_admin_email(), $user_id );
		$user_url = admin_url( "admin.php?page=wc-affiliate#/affiliates/$user_id" );

		$affiliate = new Affiliate( $user_id );

		$subject = apply_filters( 'wc_affiliate_email_admin_application_subject', wca_get_admin_application_subject(), $user_id, $affiliate_data );
		$message = apply_filters( 'wc_affiliate_email_admin_application_message', wca_get_admin_application_message(), $user_id, $affiliate_data );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name'] = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$context_defaults['user_url']   = $user_url;

		return array(
			'enabled'    => true,
			'recipients' => array( $email ),
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $context_defaults,
		);
	}

	/**
	 * Compose account review email for affiliate.
	 *
	 * @param int    $user_id       User ID.
	 * @param string $review_action Review action (approve/reject).
	 *
	 * @return array Email content data.
	 */
	public function compose_account_review_affiliate( int $user_id, string $review_action ): array {
		$affiliate = new Affiliate( $user_id );
		$email     = $affiliate->get( 'user_email' );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name'] = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );

		if ( 'approve' === $review_action ) {
			$enable = apply_filters( 'wc_affiliate_email_affiliate_approval_enabled', wca_is_affiliate_approval_email_enabled(), $user_id, $review_action );

			if ( ! $enable ) {
				return array( 'enabled' => false );
			}

			$subject = apply_filters( 'wc_affiliate_email_affiliate_approval_subject', wca_get_affiliate_approval_subject(), $user_id, $review_action );
			$message = apply_filters( 'wc_affiliate_email_affiliate_approval_message', wca_get_affiliate_approval_message(), $user_id, $review_action );

			return array(
				'enabled'    => true,
				'recipients' => array( $email ),
				'subject'    => $subject,
				'content'    => $message,
				'context'    => $context_defaults,
			);
		}

		if ( 'reject' === $review_action ) {
			$enable = apply_filters( 'wc_affiliate_email_affiliate_rejection_enabled', wca_is_affiliate_rejection_email_enabled(), $user_id, $review_action );

			if ( ! $enable ) {
				return array( 'enabled' => false );
			}

			$subject = apply_filters( 'wc_affiliate_email_affiliate_rejection_subject', wca_get_affiliate_rejection_subject(), $user_id, $review_action );
			$message = apply_filters( 'wc_affiliate_email_affiliate_rejection_message', wca_get_affiliate_rejection_message(), $user_id, $review_action );

			return array(
				'enabled'    => true,
				'recipients' => array( $email ),
				'subject'    => $subject,
				'content'    => $message,
				'context'    => $context_defaults,
			);
		}

		return array( 'enabled' => false );
	}

	/**
	 * Compose commission earned email for affiliate.
	 *
	 * @param int   $user_id       Affiliate user ID.
	 * @param int   $order_id      Order ID.
	 * @param array $referral_data Referral data.
	 *
	 * @return array Email content data.
	 */
	public function compose_add_credit_affiliate( int $user_id, int $order_id, array $referral_data ): array {
		$enable = apply_filters( 'wc_affiliate_email_affiliate_commission_enabled', wca_is_affiliate_commission_email_enabled(), $user_id, $order_id, $referral_data );

		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		$affiliate = new Affiliate( $user_id );
		if ( ! $affiliate->exists() ) {
			return array( 'enabled' => false );
		}

		$email  = $affiliate->get( 'user_email' );
		$amount = $referral_data['commission'] ?? 0;

		$subject = apply_filters( 'wc_affiliate_email_affiliate_commission_subject', wca_get_affiliate_commission_subject(), $user_id, $order_id, $referral_data );
		$message = apply_filters( 'wc_affiliate_email_affiliate_commission_message', wca_get_affiliate_commission_message(), $user_id, $order_id, $referral_data );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name']   = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$context_defaults['amount']       = $amount;
		$context_defaults['commission']   = $amount;
		$context_defaults['order_id']     = $order_id;
		$context_defaults['earning_time'] = wp_date( 'd-m-Y-H-i-s A', $referral_data['time'] );

		return array(
			'enabled'    => true,
			'recipients' => array( $email ),
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $context_defaults,
		);
	}

	/**
	 * Compose commission paid email for admin.
	 *
	 * @param int   $user_id       Affiliate user ID.
	 * @param int   $order_id      Order ID.
	 * @param array $referral_data Referral data.
	 *
	 * @return array Email content data.
	 */
	public function compose_add_credit_admin( int $user_id, int $order_id, array $referral_data ): array {
		$enable = apply_filters( 'wc_affiliate_email_admin_commission_enabled', wca_is_admin_commission_email_enabled(), $user_id, $order_id, $referral_data );

		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		$affiliate = new Affiliate( $user_id );
		if ( ! $affiliate->exists() ) {
			return array( 'enabled' => false );
		}

		$amount = $referral_data['commission'] ?? 0;

		$email   = apply_filters( 'wc_affiliate_email_admin_commission_recipients', wca_admin_email(), $user_id, $order_id, $referral_data );
		$subject = apply_filters( 'wc_affiliate_email_admin_commission_subject', wca_get_admin_commission_subject(), $user_id, $order_id, $referral_data );
		$message = apply_filters( 'wc_affiliate_email_admin_commission_message', wca_get_admin_commission_message(), $user_id, $order_id, $referral_data );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name']   = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$context_defaults['amount']       = $amount;
		$context_defaults['commission']   = $amount;
		$context_defaults['order_id']     = $order_id;
		$context_defaults['earning_time'] = wp_date( 'd-m-Y-H-i-s A', $referral_data['time'] );

		return array(
			'enabled'    => true,
			'recipients' => array( $email ),
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $context_defaults,
		);
	}

	/**
	 * Compose payout request email for affiliate.
	 *
	 * @param Affiliate $affiliate Affiliate model instance.
	 * @param float     $amount    Amount to be paid out.
	 *
	 * @return array Email content data.
	 */
	public function compose_request_payout_affiliate( Affiliate $affiliate, float $amount ): array {
		$enable = apply_filters( 'wc_affiliate_email_affiliate_payout_request_enabled', wca_is_affiliate_payout_request_email_enabled(), $affiliate, $amount );

		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		$user_id = $affiliate->get( 'user_id' );
		$email   = $affiliate->get( 'user_email' );
		$emails  = apply_filters( 'wc_affiliate_email_affiliate_payout_request_recipients', array( $email ), $user_id, $amount );

		$subject = apply_filters( 'wc_affiliate_email_affiliate_payout_request_subject', wca_get_affiliate_payout_request_subject(), $user_id, $amount );
		$message = apply_filters( 'wc_affiliate_email_affiliate_payout_request_message', wca_get_affiliate_payout_request_message(), $user_id, $amount );

		$payout_request_at = $affiliate->get( 'payout_request_at', '' );

		$defaults = $affiliate->to_array();

		$defaults['amount']      = $amount;
		$defaults['first_name']  = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$defaults['payout_time'] = wp_date( 'd-m-Y-H-i-s A', $payout_request_at );

		return array(
			'enabled'    => true,
			'recipients' => $emails,
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $defaults,
		);
	}

	/**
	 * Compose payout request email for admin.
	 *
	 * @param Affiliate $affiliate Affiliate model instance.
	 * @param float     $amount    Amount to be paid out.
	 *
	 * @return array Email content data.
	 */
	public function compose_request_payout_admin( Affiliate $affiliate, float $amount ): array {
		$enable = apply_filters( 'wc_affiliate_email_admin_payout_request_enabled', wca_is_admin_payout_request_email_enabled(), $affiliate, $amount );

		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		$user_id = $affiliate->get( 'user_id' );
		$email   = apply_filters( 'wc_affiliate_email_admin_payout_request_recipients', wca_admin_email(), $affiliate, $amount );

		$subject = apply_filters( 'wc_affiliate_email_admin_payout_request_subject', wca_get_admin_payout_request_subject(), $user_id, $amount );
		$message = apply_filters( 'wc_affiliate_email_admin_payout_request_message', wca_get_admin_payout_request_message(), $user_id, $amount );

		$payout_request_at = $affiliate->get( 'payout_request_at', '' );

		$defaults = $affiliate->to_array();

		$defaults['amount']      = $amount;
		$defaults['first_name']  = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$defaults['payout_time'] = wp_date( 'd-m-Y-H-i-s A', $payout_request_at );
		$defaults['order_id']    = 'N/A';

		return array(
			'enabled'    => true,
			'recipients' => array( $email ),
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $defaults,
		);
	}

	/**
	 * Compose payout processed email for affiliate.
	 *
	 * @param int   $affiliate_id     Affiliate ID.
	 * @param float $amount           Payout amount.
	 * @param array $transaction_data Transaction data array.
	 *
	 * @return array Email content data.
	 */
	public function compose_payout_processed( int $affiliate_id, float $amount, array $transaction_data ): array {
		$enable = apply_filters( 'wc_affiliate_email_payout_processed_enabled', wca_is_payout_processed_email_enabled(), $affiliate_id, $amount, $transaction_data );

		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		$affiliate = new Affiliate( $affiliate_id );
		$email     = $affiliate->get( 'user_email' );
		$emails    = apply_filters( 'wc_affiliate_email_payout_processed_recipients', array( $email ), $affiliate_id );

		$subject = apply_filters( 'wc_affiliate_email_payout_processed_subject', wca_get_payout_processed_subject(), $affiliate_id );
		$message = apply_filters( 'wc_affiliate_email_payout_processed_message', wca_get_payout_processed_message(), $affiliate_id );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name']      = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$context_defaults['amount']          = $amount;
		$context_defaults['processing_time'] = $transaction_data['formatted_process_time'];
		$context_defaults['order_id']        = 'N/A';

		return array(
			'enabled'    => true,
			'recipients' => $emails,
			'subject'    => $subject,
			'content'    => $message,
			'context'    => array_merge( $context_defaults, $transaction_data ),
		);
	}

	/**
	 * Compose transaction created email for admin.
	 *
	 * @param int         $transaction_id Transaction ID.
	 * @param Transaction $transaction    Transaction model instance.
	 *
	 * @return array Email content data.
	 */
	public function compose_transaction_created_admin( int $transaction_id, Transaction $transaction ): array {
		$enable = apply_filters( 'wc_affiliate_email_admin_transaction_enabled', wca_is_admin_transaction_email_enabled(), $transaction_id, $transaction );

		if ( ! $enable || ! $transaction->exists() ) {
			return array( 'enabled' => false );
		}

		$affiliate = $transaction->get_affiliate();
		if ( ! $affiliate instanceof Affiliate || ! $affiliate->exists() ) {
			return array( 'enabled' => false );
		}

		$email = apply_filters( 'wc_affiliate_email_admin_transaction_recipients', wca_admin_email(), $transaction_id, $transaction );

		$subject = apply_filters( 'wc_affiliate_email_admin_transaction_subject', wca_get_admin_transaction_subject(), $transaction_id, $transaction );
		$message = apply_filters( 'wc_affiliate_email_admin_transaction_message', wca_get_admin_transaction_message(), $transaction_id, $transaction );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name']         = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$context_defaults['transaction_type']   = $transaction->get_payment_method_label( $transaction->get_payment_method() );
		$context_defaults['transaction_amount'] = $transaction->get_amount();
		$context_defaults['transaction_status'] = $transaction->get_status();

		return array(
			'enabled'    => true,
			'recipients' => array( $email ),
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $context_defaults,
		);
	}

	/**
	 * Compose paid referral email for affiliate.
	 *
	 * @param int         $referral_id Referral ID.
	 * @param Referral    $referral    Referral model instance.
	 * @param Transaction $transaction Transaction model instance.
	 *
	 * @return array Email content data.
	 */
	public function compose_paid_referral_affiliate( int $referral_id, Referral $referral, Transaction $transaction ): array {
		$enable = apply_filters( 'wc_affiliate_email_affiliate_paid_referral_enabled', wca_is_affiliate_paid_referral_email_enabled(), $referral_id, $referral, $transaction );

		if ( ! $enable || ! $referral->exists() ) {
			return array( 'enabled' => false );
		}

		$affiliate = $referral->get_affiliate();
		if ( ! $affiliate instanceof Affiliate || ! $affiliate->exists() ) {
			return array( 'enabled' => false );
		}

		$email  = $affiliate->get( 'user_email' );
		$emails = apply_filters( 'wc_affiliate_email_affiliate_paid_referral_recipients', array( $email ), $referral_id, $referral );

		$subject = apply_filters( 'wc_affiliate_email_affiliate_paid_referral_subject', wca_get_affiliate_paid_referral_subject(), $referral_id, $referral );
		$message = apply_filters( 'wc_affiliate_email_affiliate_paid_referral_message', wca_get_affiliate_paid_referral_message(), $referral_id, $referral );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name']          = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$context_defaults['referral_id']         = $referral_id;
		$context_defaults['referral_order_id']   = $referral->get_order_id();
		$context_defaults['referral_commission'] = $referral->get_commission();
		$context_defaults['referral_status']     = $referral->get_status();
		$context_defaults['transaction_type']    = $transaction->get_payment_method_label( $transaction->get_payment_method() );
		$context_defaults['transaction_amount']  = $transaction->get_amount();
		$context_defaults['transaction_status']  = $transaction->get_status();

		return array(
			'enabled'    => true,
			'recipients' => $emails,
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $context_defaults,
		);
	}

	/**
	 * Compose paid referral email for admin.
	 *
	 * @param int         $referral_id Referral ID.
	 * @param Referral    $referral    Referral model instance.
	 * @param Transaction $transaction Transaction model instance.
	 *
	 * @return array Email content data.
	 */
	public function compose_paid_referral_admin( int $referral_id, Referral $referral, Transaction $transaction ): array {
		$enable = apply_filters( 'wc_affiliate_email_admin_paid_referral_enabled', wca_is_admin_paid_referral_email_enabled(), $referral_id, $referral, $transaction );

		if ( ! $enable ) {
			return array( 'enabled' => false );
		}

		// Get referral data from the referral model.
		$referral = new Referral( $referral_id );
		if ( ! $referral->exists() ) {
			return array( 'enabled' => false );
		}

		$affiliate = $referral->get_affiliate();
		if ( ! $affiliate instanceof Affiliate || ! $affiliate->exists() ) {
			return array( 'enabled' => false );
		}

		$email   = apply_filters( 'wc_affiliate_email_admin_paid_referral_recipients', wca_admin_email(), $referral_id, $referral, $transaction );
		$subject = apply_filters( 'wc_affiliate_email_admin_paid_referral_subject', wca_get_admin_paid_referral_subject(), $referral_id, $referral );
		$message = apply_filters( 'wc_affiliate_email_admin_paid_referral_message', wca_get_admin_paid_referral_message(), $referral_id, $referral );

		$context_defaults = $affiliate->to_array();

		$context_defaults['first_name']          = $affiliate->get( 'first_name', $affiliate->get( 'display_name' ) );
		$context_defaults['referral_id']         = $referral_id;
		$context_defaults['referral_order_id']   = $referral->get_order_id();
		$context_defaults['referral_commission'] = $referral->get_commission();
		$context_defaults['referral_status']     = $referral->get_status();
		$context_defaults['transaction_type']    = $transaction->get_payment_method_label( $transaction->get_payment_method() );
		$context_defaults['transaction_amount']  = $transaction->get_amount();
		$context_defaults['transaction_status']  = $transaction->get_status();

		return array(
			'enabled'    => true,
			'recipients' => array( $email ),
			'subject'    => $subject,
			'content'    => $message,
			'context'    => $context_defaults,
		);
	}

	// **
	// * Compose daily summary email for admin.
	// *
	// * @param string $email Admin email address.
	// * @param array  $stats Daily statistics.
	// * @param string $date  Date for the summary.
	// *
	// * @return array Email content data.
	// */
	// public function compose_daily_summary( string $email, array $stats, string $date ): array {
	// * translators: %s is the date */
	// $subject = apply_filters( 'wc_affiliate_email_subject_daily_summary', sprintf( __( 'WC Affiliate Daily Summary - %s', 'wc-affiliate' ), wp_date( 'F j, Y', strtotime( $date ) ) ), 'daily_summary' );
	//
	// Prepare template variables.
	// $template_vars = apply_filters(
	// 'wc_affiliate_email_daily_summary_content_data',
	// array(
	// 'title'                => apply_filters( 'wc_affiliate_email_content_daily_summary_title', __( 'Daily Affiliate Summary', 'wc-affiliate' ), 'daily_summary' ),
	// 'date_label'           => apply_filters( 'wc_affiliate_email_content_daily_summary_date_label', __( 'Date:', 'wc-affiliate' ), 'daily_summary' ),
	// 'date'                 => wp_date( 'F j, Y', strtotime( $date ) ),
	// 'stats'                => $stats,
	// 'visits_label'         => apply_filters( 'wc_affiliate_email_content_daily_summary_visits_label', __( 'Total Visits:', 'wc-affiliate' ), 'daily_summary' ),
	// 'referrals_label'      => apply_filters( 'wc_affiliate_email_content_daily_summary_referrals_label', __( 'Total Referrals:', 'wc-affiliate' ), 'daily_summary' ),
	// 'commissions_label'    => apply_filters( 'wc_affiliate_email_content_daily_summary_commissions_label', __( 'Total Commissions:', 'wc-affiliate' ), 'daily_summary' ),
	// 'top_affiliates_title' => apply_filters( 'wc_affiliate_email_content_daily_summary_top_affiliates_title', __( 'Top Affiliates', 'wc-affiliate' ), 'daily_summary' ),
	// 'top_affiliates'       => $stats['top_affiliates'] ?? array(),
	// 'referrals_text'       => apply_filters( 'wc_affiliate_email_content_daily_summary_referrals_text', __( 'referrals', 'wc-affiliate' ), 'daily_summary' ),
	// ),
	// $email,
	// $stats,
	// $date
	// );
	//
	// $message = wca_get_template( 'messages/daily-summary', 'views/emails', $template_vars );
	// $message = apply_filters( 'wc_affiliate_email_content_daily_summary', $message, $stats, $date, $email );
	//
	// return array(
	// 'enabled'    => true,
	// 'recipients' => array( $email ),
	// 'subject'    => $subject,
	// 'content'    => $message,
	// 'context'    => array(
	// 'email' => $email,
	// 'stats' => $stats,
	// 'date'  => $date,
	// ),
	// );
	// }
	//
	// **
	// * Compose weekly report email for admin.
	// *
	// * @param string $email        Admin email address.
	// * @param array  $weekly_stats Weekly statistics.
	// * @param string $start_date   Start date.
	// * @param string $end_date     End date.
	// *
	// * @return array Email content data.
	// */
	// public function compose_weekly_report( string $email, array $weekly_stats, string $start_date, string $end_date ): array {
	// * translators: %1$s: Start date, %2$s: End date */
	// $subject = apply_filters( 'wc_affiliate_email_subject_weekly_report', sprintf( __( 'WC Affiliate Weekly Report - %1$s to %2$s', 'wc-affiliate' ), $start_date, $end_date ), 'weekly_report' );
	//
	// Prepare template variables.
	// $template_vars = apply_filters(
	// 'wc_affiliate_email_weekly_report_content_data',
	// array(
	// 'title'                => apply_filters( 'wc_affiliate_email_content_weekly_report_title', __( 'Weekly Affiliate Report', 'wc-affiliate' ), 'weekly_report' ),
	// 'period_label'         => apply_filters( 'wc_affiliate_email_content_weekly_report_period_label', __( 'Period:', 'wc-affiliate' ), 'weekly_report' ),
	// 'start_date'           => $start_date,
	// 'end_date'             => $end_date,
	// 'weekly_stats'         => $weekly_stats,
	// 'visits_label'         => apply_filters( 'wc_affiliate_email_content_weekly_report_visits_label', __( 'Total Visits:', 'wc-affiliate' ), 'weekly_report' ),
	// 'referrals_label'      => apply_filters( 'wc_affiliate_email_content_weekly_report_referrals_label', __( 'Total Referrals:', 'wc-affiliate' ), 'weekly_report' ),
	// 'commissions_label'    => apply_filters( 'wc_affiliate_email_content_weekly_report_commissions_label', __( 'Total Commissions:', 'wc-affiliate' ), 'weekly_report' ),
	// 'sales_label'          => apply_filters( 'wc_affiliate_email_content_weekly_report_sales_label', __( 'Total Sales:', 'wc-affiliate' ), 'weekly_report' ),
	// 'conversion_label'     => apply_filters( 'wc_affiliate_email_content_weekly_report_conversion_label', __( 'Conversion Rate:', 'wc-affiliate' ), 'weekly_report' ),
	// 'top_performers_title' => apply_filters( 'wc_affiliate_email_content_weekly_report_top_performers_title', __( 'Top Performing Affiliates', 'wc-affiliate' ), 'weekly_report' ),
	// 'referrals_text'       => apply_filters( 'wc_affiliate_email_content_weekly_report_referrals_text', __( 'referrals', 'wc-affiliate' ), 'weekly_report' ),
	// 'commissions_text'     => apply_filters( 'wc_affiliate_email_content_weekly_report_commissions_text', __( 'commissions', 'wc-affiliate' ), 'weekly_report' ),
	// ),
	// $email,
	// $weekly_stats,
	// $start_date,
	// $end_date
	// );
	//
	// $message = wca_get_template( 'messages/weekly-report', 'views/emails', $template_vars );
	// $message = apply_filters( 'wc_affiliate_email_content_weekly_report', $message, $weekly_stats, $start_date, $end_date, $email );
	//
	// return array(
	// 'enabled'    => true,
	// 'recipients' => array( $email ),
	// 'subject'    => $subject,
	// 'content'    => $message,
	// 'context'    => array(
	// 'email'        => $email,
	// 'weekly_stats' => $weekly_stats,
	// 'start_date'   => $start_date,
	// 'end_date'     => $end_date,
	// ),
	// );
	// }

	/**
	 * Compose verification resend email for affiliate.
	 *
	 * @param mixed $user WP_User object.
	 *
	 * @return array Email content data.
	 */
	public function compose_verification_resend( $user ): array {
		if ( ! wca_is_email_validation_enabled() ) {
			return array( 'enabled' => false );
		}

		$email      = $user->data->user_email;
		$first_name = ! empty( $user->data->display_name ) ? $user->data->display_name : 'there';
		$site_name  = get_bloginfo( 'name' );

		$dashboard_url   = wca_get_dashboard();
		$validation_data = array(
			'id'    => $user->ID,
			'email' => $user->data->user_email,
			'time'  => time(),
		);

		$validation_data = wp_json_encode( $validation_data );
		$validation_data = wca_ncrypt()->encrypt( $validation_data );
		$verify_url      = add_query_arg( 'validate', $validation_data, $dashboard_url );

		// Prepare highlight content.
		$highlight_content  = '<h3 style="margin-top: 0; color: #92400e;">🔒 Email Verification Required</h3>';
		$highlight_content .= '<p>Please verify your email address to activate your affiliate account and unlock all features.</p>';

		// Generate content sections.
		$greeting  = apply_filters( 'wc_affiliate_email_greeting_content', '', $first_name );
		$highlight = apply_filters( 'wc_affiliate_email_highlight_content', '', $highlight_content );
		$button    = apply_filters( 'wc_affiliate_email_button_content', '', $verify_url, 'Verify My Email Now' );
		$signature = apply_filters( 'wc_affiliate_email_signature_content', '', $site_name );

		$content = wca_get_template(
			'messages/affiliate-verification-resend',
			'views/emails',
			array(
				'greeting'   => $greeting,
				'highlight'  => $highlight,
				'button'     => $button,
				'verify_url' => $verify_url,
				'signature'  => $signature,
			)
		);

		$subject = __( 'Email Verification - Complete Your Affiliate Registration', 'wc-affiliate' );

		return array(
			'enabled'    => true,
			'recipients' => array( $email ),
			'subject'    => $subject,
			'content'    => $content,
			'context'    => array(
				'first_name'   => ! empty( $user->first_name ) ? $user->first_name : $user->display_name,
				'last_name'    => $user->last_name,
				'user_email'   => $user->user_email,
				'display_name' => $user->display_name,
				'user_id'      => $user->ID,
			),
		);
	}
}
