<?php

namespace WC_Affiliate\Controllers\Common;

use WC_Order;

defined( 'ABSPATH' ) || exit;

/**
 * Common Init Controller for WC Affiliate Plugin.
 *
 * Handles common initialization tasks including email verification
 * and modal display functionality for both admin and frontend.
 *
 * @since 1.0.0
 */
class Init {

	/**
	 * Credit amount applied via EasyCommerce discount filter, shared with deduction hook.
	 *
	 * @var float
	 */
	private float $ec_credit_amount = 0.0;

	/**
	 * Controller constructor
	 *
	 * Initializes the common controller and registers necessary hooks
	 */
	public function __construct() {
		$this->register_hooks();
	}

	/**
	 * Register hooks for common controller
	 *
	 * Registers all necessary hooks for setup wizard and pay-with-credits functionality
	 *
	 * @return void
	 */
	protected function register_hooks(): void {
		add_action( 'init', array( $this, 'track_settings_reloaded' ) );
		add_action( 'init', array( $this, 'hide_admin_bar_for_affiliates' ) );
		add_action( 'after_setup_theme', array( $this, 'hide_admin_bar_for_affiliates' ) );
		add_action( 'admin_init', array( $this, 'restrict_admin_access_by_affiliates' ) );

		add_filter( 'wc_affiliate_legacy_option_value', array( $this, 'modify_legacy_option_value' ), 10, 2 );
		add_filter( 'wc_affiliate_setup_wizard_updated_values', array( $this, 'setup_wizard_updated_values' ), 10, 4 );
		add_action( 'wc_affiliate_complete_wizard', array( $this, 'complete_wizard' ) );

		// Pay with credits functionality.
		if ( wca_can_pay_with_credit() ) {
			// WooCommerce hooks.
			add_action( 'woocommerce_review_order_before_payment', array( $this, 'pay_with_credits_field' ) );
			add_filter( 'woocommerce_update_order_review_fragments', array( $this, 'pay_with_credits_fragment' ) );
			add_action( 'woocommerce_cart_calculate_fees', array( $this, 'apply_credit_discount' ) );
			add_action( 'woocommerce_checkout_update_order_review', array( $this, 'update_credit_session_from_review' ) );
			add_action( 'woocommerce_checkout_process', array( $this, 'process_credit_payment' ) );
			add_action( 'woocommerce_checkout_order_created', array( $this, 'deduct_credits_on_order' ) );

			// EasyCommerce hooks.
			add_action( 'easycommerce-before_cart_payment_methods', array( $this, 'ec_pay_with_credits_field' ) );
			add_filter( 'easycommerce-calculate_cart_total', array( $this, 'ec_apply_credit_discount' ), 10, 2 );
			add_action( 'easycommerce_after_create_order', array( $this, 'ec_deduct_credits_on_order' ), 10, 3 );
		}

		add_action( 'wc_affiliate_payout_paid', array( $this, 'update_payout_data' ), 10, 2 );
		add_action( 'wc_affiliate_notice_dismissed', array( $this, 'save_notice_dismissed_data' ) );
	}

	public function track_settings_reloaded(): void {
		$is_wizard_completed = get_option( 'wc_affiliate_wizard_completed', false );
		$dashboard_page_id   = wca_get_dashboard_page_id();

		if ( (bool) $is_wizard_completed && 0 === $dashboard_page_id ) {
			add_option( 'wc_affiliate_settings_page_reloaded', true, '', false );
		} else {
			delete_option( 'wc_affiliate_settings_page_reloaded' );
		}
	}

	/**
	 * Modify legacy option values for backward compatibility.
	 *
	 * Transforms outdated option values to their current equivalents. Currently handles
	 * the conversion of 'payable_amount' to 'order_total' for the commission_base setting.
	 *
	 * @since 1.0.0
	 *
	 * @param mixed  $value      The option value to be modified.
	 * @param string $legacy_key The legacy option key being checked.
	 *
	 * @return mixed The modified option value, or original value if no modification is needed.
	 */
	public function modify_legacy_option_value( $value, string $legacy_key ) {
		if ( 'commission_base' === $legacy_key && 'payable_amount' === $value ) {
			$value = 'order_total';
		}

		return $value;
	}

	/**
	 * Restrict admin access for affiliate users based on settings.
	 */
	public function restrict_admin_access_by_affiliates() {
		/**
		 * Action hook fired before admin access restriction check.
		 */
		do_action( 'wc_affiliate_restrict_admin_access_before' );

		if ( wca_should_restrict_admin_access() && is_admin() && ! wp_doing_ajax() && wca_is_affiliate() ) {
			/**
			 * Action hook fired before admin access redirect.
			 */
			do_action( 'wc_affiliate_admin_access_redirect_before' );

			wp_safe_redirect( wca_get_dashboard() );
			exit;
		}

		/**
		 * Action hook fired after admin access restriction check.
		 */
		do_action( 'wc_affiliate_restrict_admin_access_after' );
	}

	/**
	 * Hide admin bar for affiliate users.
	 */
	public function hide_admin_bar_for_affiliates() {
		if ( ! is_admin() && wca_is_affiliate() ) {
			show_admin_bar( false );
		}
	}

	/**
	 * Handle setup wizard updates.
	 *
	 * Processes setup wizard completion and creates dashboard page if needed
	 *
	 * @param array  $page_data     Form values.
	 * @param string $page          Current wizard page.
	 * @param array  $pages         Wizard pages configuration.
	 * @param array  $field_mapping Field mapping configuration.
	 *
	 * @return array
	 */
	public function setup_wizard_updated_values( array $page_data, string $page, array $pages, array $field_mapping ): array {
		if ( 'pages' !== $page ) {
			return $page_data;
		}

		$page_option_type = $page_data['pages']['dashboard_page']['page_option'] ?? 'create';
		$page_title       = $page_data['pages']['dashboard_page']['new_page_title'] ?? __( 'Affiliate Dashboard', 'wc-affiliate' );

		if ( 'create' === $page_option_type && ! empty( $page_title ) ) {
			$post_id = wp_insert_post(
				array(
					'post_title'     => $page_title,
					'post_type'      => 'page',
					'post_status'    => 'publish',
					'post_content'   => '<!-- wp:shortcode -->[wc-affiliate-dashboard]<!-- /wp:shortcode -->',
					'post_excerpt'   => 'This page serves as the affiliate dashboard where affiliates can manage their account, view referrals, track commissions, and access their affiliate tools and resources.',
					'comment_status' => 'closed',
				)
			);

			if ( is_wp_error( $post_id ) ) {
				return $page_data;
			}

			// Update the existing page ID in setup wizard options.
			$page_data['pages']['dashboard_page']['page_option']   = 'existing';
			$page_data['pages']['dashboard_page']['existing_page'] = (string) $post_id;

			wca_update_setup_wizard_options( '', $page_data, $pages, $field_mapping );
			wca_update_settings( 'basic', 'general', array( 'dashboard_page' => (string) $post_id ) );

			do_action( 'wc_affiliate_setup_wizard_dashboard_page_created', $page_data );
		}

		return $page_data;
	}

	/**
	 * Complete the setup wizard and send user data to external service.
	 *
	 * Processes setup wizard completion by extracting user information from
	 * the form data and sending it to the Pluggable.io CRM service for contact
	 * creation or update.
	 *
	 * @since 3.0.6
	 *
	 * @param array $form_data The setup wizard form data containing user information.
	 *                         Expected to contain 'crm' key with 'crm_email', 'crm_first_name',
	 *                         and 'crm_last_name' fields.
	 *
	 * @return void
	 */
	public function complete_wizard( array $form_data ): void {
		$user_email      = $form_data['crm']['crm_email'] ?? get_bloginfo( 'admin_email' );
		$user_first_name = $form_data['crm']['crm_first_name'] ?? '';
		$user_last_name  = $form_data['crm']['crm_last_name'] ?? '';

		$user_full_name = $user_first_name . ' ' . $user_last_name;
		$user_country   = substr( get_option( 'woocommerce_default_country', '' ), 0, 2 );
		$user_site_url  = get_bloginfo( 'url' );

		$request_url = 'https://my.pluggable.io/?fluentcrm=1&route=contact&hash=5ae65e96-4523-4c70-9781-67fc7de918ba';

		wp_remote_post(
			$request_url,
			array(
				'body' => array(
					'email'      => $user_email,
					'first_name' => $user_first_name,
					'last_name'  => $user_last_name,
					'full_name'  => $user_full_name,
					'country'    => $user_country,
					'site_url'   => $user_site_url,
				),
			)
		);
	}

	/**
	 * Add pay with credits field to EasyCommerce checkout
	 *
	 * Displays the affiliate credits payment option during EasyCommerce checkout.
	 *
	 * @return void
	 */
	public function ec_pay_with_credits_field(): void {
		if ( ! wca_affiliate_can_pay_with_credit() ) {
			return;
		}

		if ( ! class_exists( '\EasyCommerce\Models\Cart' ) ) {
			return;
		}

		$user_id     = get_current_user_id();
		$cart        = new \EasyCommerce\Models\Cart();
		$cart_total  = (float) $cart->get_amount();
		$credit_info = $this->get_eligible_credit_info( $user_id, $cart_total );

		if ( ! $credit_info['can_use'] ) {
			return;
		}

		echo wca_get_template( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			'pay-with-credits',
			'views/front',
			array(
				'checked'         => '',
				'currency_symbol' => wca_get_currency_symbol(),
				'credit_info'     => $credit_info,
			),
			true
		);
	}

	/**
	 * Apply affiliate credit discount to EasyCommerce cart total
	 *
	 * Reduces the cart total by the eligible credit amount when the affiliate
	 * has chosen to pay with credits.
	 *
	 * @param float $total Cart total before discount.
	 * @param mixed $cart  EasyCommerce Cart object.
	 *
	 * @return float Adjusted cart total.
	 */
	public function ec_apply_credit_discount( float $total, $cart ): float {
		if ( ! wca_affiliate_can_pay_with_credit() ) {
			return $total;
		}

		if ( empty( $_POST['wca_pay_with_credits'] ) || '1' !== sanitize_key( wp_unslash( $_POST['wca_pay_with_credits'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
			return $total;
		}

		$user_id     = get_current_user_id();
		$credit_info = $this->get_eligible_credit_info( $user_id, $total );

		if ( ! $credit_info['can_use'] ) {
			return $total;
		}

		$this->ec_credit_amount = $credit_info['eligible_amount'];

		return max( 0.0, $total - $credit_info['eligible_amount'] );
	}

	/**
	 * Deduct affiliate credits after EasyCommerce order creation
	 *
	 * Marks used referral records as paid and records a credit transaction
	 * when an EasyCommerce order is completed with credit payment.
	 *
	 * @param int   $order_id    Newly created order ID.
	 * @param array $params      Merged request + POST + GET params.
	 * @param int   $customer_id Customer/affiliate user ID.
	 *
	 * @return void
	 */
	public function ec_deduct_credits_on_order( int $order_id, array $params, int $customer_id ): void {
		if ( empty( $params['wca_pay_with_credits'] ) || '1' !== (string) $params['wca_pay_with_credits'] ) {
			return;
		}

		if ( ! $customer_id || $this->ec_credit_amount <= 0 ) {
			return;
		}

		global $wpdb;

		$wpdb->insert(
			$wpdb->prefix . 'wca_transactions',
			array(
				'affiliate'      => $customer_id,
				'amount'         => -$this->ec_credit_amount,
				'payment_method' => 'credit_payment',
				'txn_id'         => 'ORDER-' . $order_id,
				'status'         => 'completed',
				'request_at'     => time(),
				'process_at'     => time(),
			),
			array( '%d', '%f', '%s', '%s', '%s', '%d', '%d' )
		); // phpcs:ignore WordPress.DB.DirectDatabaseQuery

		$this->mark_credits_as_used( $customer_id, $this->ec_credit_amount, $order_id );

		$this->ec_credit_amount = 0.0;
	}

	/**
	 * Get the pre-fee cart total for credit eligibility calculation.
	 *
	 * WC()->cart->get_total('edit') returns 0 inside woocommerce_cart_calculate_fees
	 * because the grand total is reset at the start of calculate_totals(). Sum the
	 * stable pre-fee components directly to avoid this.
	 *
	 * @return float
	 */
	private function get_cart_pre_fee_total(): float {
		if ( ! WC()->cart ) {
			return 0.0;
		}

		return max(
			0.0,
			WC()->cart->get_subtotal()
				+ WC()->cart->get_shipping_total()
				+ WC()->cart->get_taxes_total()
				- WC()->cart->get_discount_total()
		);
	}

	/**
	 * Get user eligible credit amount for payment
	 *
	 * Only approved referral commissions count toward the available credit balance.
	 * Calculates how much affiliate credit a user can use for checkout.
	 *
	 * @param int   $user_id    User ID.
	 * @param float $cart_total Cart total amount.
	 *
	 * @return array {
	 *     Credit information array.
	 *
	 * @type float  $total_credits   Total available approved credits.
	 * @type float  $eligible_amount Amount that can be used for this cart.
	 * @type bool   $can_use         Whether credits can be used.
	 *                               }
	 */
	protected function get_eligible_credit_info( int $user_id, float $cart_total ): array {
		global $wpdb;

		// Sum only approved commissions — pending/paid referrals are excluded.
		$approved_amount = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT SUM(commission) FROM {$wpdb->prefix}wca_referrals WHERE affiliate = %d AND payment_status = 'approved'",
				$user_id
			)
		);

		$approved_amount = (float) $approved_amount;

		// Eligible amount cannot exceed the cart total or the approved credit balance.
		$eligible_amount = min( $cart_total, $approved_amount );

		return array(
			'total_credits'   => $approved_amount,
			'eligible_amount' => $eligible_amount,
			'can_use'         => $eligible_amount > 0,
		);
	}

	/**
	 * Add pay with credits field to checkout
	 *
	 * Displays the affiliate credits payment option during WooCommerce checkout
	 *
	 * @return void
	 */
	public function pay_with_credits_field(): void {
		if ( ! wca_affiliate_can_pay_with_credit() || ! WC()->cart ) {
			echo '<div id="wca-pay-with-credits-wrapper" class="wca-pay-with-credits"></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			return;
		}

		$user_id     = get_current_user_id();
		$credit_info = $this->get_eligible_credit_info( $user_id, $this->get_cart_pre_fee_total() );

		if ( ! $credit_info['can_use'] ) {
			echo '<div id="wca-pay-with-credits-wrapper" class="wca-pay-with-credits"></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			return;
		}

		$currency_symbol = wca_get_currency_symbol();
		$checked         = WC()->session->get( 'wca_pay_with_credits' ) ? 'checked' : '';

		echo wca_get_template( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			'pay-with-credits',
			'views/front',
			array(
				'checked'         => $checked,
				'currency_symbol' => $currency_symbol,
				'credit_info'     => $credit_info,
			),
			true
		);
	}

	/**
	 * Inject pay-with-credits widget as a WooCommerce checkout fragment.
	 *
	 * woocommerce/templates/checkout/payment.php guards woocommerce_review_order_before_payment
	 * with `! wp_doing_ajax()`, so the checkbox is never re-rendered during order-review AJAX.
	 * This filter pushes a fresh fragment so the checkbox state stays in sync.
	 *
	 * @param array $fragments Key → HTML map of page fragments to replace.
	 *
	 * @return array
	 */
	public function pay_with_credits_fragment( array $fragments ): array {
		if ( ! wca_affiliate_can_pay_with_credit() || ! WC()->cart ) {
			$fragments['#wca-pay-with-credits-wrapper'] = '<div id="wca-pay-with-credits-wrapper" class="wca-pay-with-credits"></div>';
			return $fragments;
		}

		$user_id     = get_current_user_id();
		$credit_info = $this->get_eligible_credit_info( $user_id, $this->get_cart_pre_fee_total() );

		if ( ! $credit_info['can_use'] ) {
			$fragments['#wca-pay-with-credits-wrapper'] = '<div id="wca-pay-with-credits-wrapper" class="wca-pay-with-credits"></div>';
			return $fragments;
		}

		$checked         = WC()->session->get( 'wca_pay_with_credits' ) ? 'checked' : '';
		$currency_symbol = wca_get_currency_symbol();

		$fragments['#wca-pay-with-credits-wrapper'] = wca_get_template(
			'pay-with-credits',
			'views/front',
			array(
				'checked'         => $checked,
				'currency_symbol' => $currency_symbol,
				'credit_info'     => $credit_info,
			),
			true
		);

		return $fragments;
	}

	/**
	 * Sync pay-with-credits checkbox state to session during order review AJAX.
	 *
	 * Fires on woocommerce_checkout_update_order_review so the session is up-to-date
	 * before woocommerce_cart_calculate_fees recalculates cart totals.
	 *
	 * @param string $post_data URL-encoded POST data string from the checkout form.
	 *
	 * @return void
	 */
	public function update_credit_session_from_review( string $post_data ): void {
		parse_str( $post_data, $fields );
		$checked = ! empty( $fields['wca_pay_with_credits'] ) && '1' === (string) $fields['wca_pay_with_credits'];
		WC()->session->set( 'wca_pay_with_credits', $checked );

		if ( $checked ) {
			$user_id     = get_current_user_id();
			$credit_info = $this->get_eligible_credit_info( $user_id, $this->get_cart_pre_fee_total() );
			WC()->session->set( 'wca_credit_amount', $credit_info['eligible_amount'] );
		} else {
			WC()->session->set( 'wca_credit_amount', false );
		}
	}

	/**
	 * Apply credit discount to cart
	 *
	 * Applies affiliate credit discount to WooCommerce cart when pay-with-credits is enabled
	 *
	 * @return void
	 */
	public function apply_credit_discount(): void {
		if ( ! wca_affiliate_can_pay_with_credit() ) {
			return;
		}

		// Also check post_data from update_order_review AJAX for live total updates.
		$via_post = false;
		if ( ! empty( $_POST['post_data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
			parse_str( wp_unslash( $_POST['post_data'] ), $posted_fields ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
			$via_post = ! empty( $posted_fields['wca_pay_with_credits'] ) && '1' === (string) $posted_fields['wca_pay_with_credits'];
		}

		if ( ! $via_post && ! WC()->session->get( 'wca_pay_with_credits' ) ) {
			return;
		}

		$user_id     = get_current_user_id();
		$credit_info = $this->get_eligible_credit_info( $user_id, $this->get_cart_pre_fee_total() );

		if ( $credit_info['eligible_amount'] > 0 ) {
			WC()->cart->add_fee(
				__( 'Affiliate Credit Applied', 'wc-affiliate' ),
				-$credit_info['eligible_amount'],
				false,
				'affiliate_credit'
			);
		}
	}

	/**
	 * Process credit payment on checkout
	 *
	 * Validates and processes affiliate credit payments during WooCommerce checkout
	 *
	 * @return void
	 */
	public function process_credit_payment(): void {
		if ( ! isset( $_POST['wca_pay_with_credits'] ) || '1' !== $_POST['wca_pay_with_credits'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
			WC()->session->set( 'wca_pay_with_credits', false );

			return;
		}

		// If the affiliate is no longer eligible (e.g. status changed between AJAX
		// and order submit), silently clear credits and let Stripe charge the full amount.
		if ( ! wca_affiliate_can_pay_with_credit() ) {
			WC()->session->set( 'wca_pay_with_credits', false );

			return;
		}

		$user_id     = get_current_user_id();
		$credit_info = $this->get_eligible_credit_info( $user_id, $this->get_cart_pre_fee_total() );

		// If no credits are available (drained between AJAX and submit), silently
		// clear the session so Stripe charges the full order amount instead.
		if ( ! $credit_info['can_use'] ) {
			WC()->session->set( 'wca_pay_with_credits', false );

			return;
		}

		WC()->session->set( 'wca_pay_with_credits', true );
		WC()->session->set( 'wca_credit_amount', $credit_info['eligible_amount'] );
	}

	/**
	 * Deduct credits when order is created
	 *
	 * @param WC_Order $order Order object.
	 */
	public function deduct_credits_on_order( WC_Order $order ) {
		/**
		 * Action hook fired before credit deduction on order.
		 *
		 * @param WC_Order $order Order object.
		 */
		do_action( 'wc_affiliate_deduct_credits_before', $order );

		if ( ! WC()->session->get( 'wca_pay_with_credits' ) ) {
			return;
		}

		$user_id = get_current_user_id();

		if ( ! $user_id ) {
			return;
		}

		// Read the actual credit amount from the fee already transferred from cart to order.
		// The cart fee added by apply_credit_discount() is automatically transferred to the
		// order by WooCommerce, so we must not add a second fee here.
		$credit_amount = 0.0;
		foreach ( $order->get_fees() as $fee_item ) {
			if ( __( 'Affiliate Credit Applied', 'wc-affiliate' ) === $fee_item->get_name() ) {
				$credit_amount = abs( (float) $fee_item->get_total() );
				break;
			}
		}

		if ( ! $credit_amount ) {
			return;
		}

		// Create a transaction record for credit usage.
		global $wpdb;

		$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
			$wpdb->prefix . 'wca_transactions',
			array(
				'affiliate'      => $user_id,
				'amount'         => -$credit_amount,
				'payment_method' => 'credit_payment',
				'txn_id'         => 'ORDER-' . $order->get_id(),
				'status'         => 'completed',
				'request_at'     => time(),
				'process_at'     => time(),
			),
			array( '%d', '%f', '%s', '%s', '%s', '%d', '%d' )
		);

		// Mark approved referrals as paid for the consumed credit amount.
		$this->mark_credits_as_used( $user_id, $credit_amount, $order->get_id() );

		// Clear session.
		WC()->session->set( 'wca_pay_with_credits', false );
		WC()->session->set( 'wca_credit_amount', false );

		// Add order note.
		$order->add_order_note(
			sprintf(
			/* translators: %s: credit amount */
				__( 'Affiliate credits applied: %s', 'wc-affiliate' ),
				wc_price( $credit_amount )
			)
		);

		/**
		 * Action hook fired after credit deduction on order.
		 *
		 * @param WC_Order $order         Order object.
		 * @param int      $user_id       User ID.
		 * @param float    $credit_amount Credit amount deducted.
		 */
		do_action( 'wc_affiliate_deduct_credits_after', $order, $user_id, $credit_amount );
	}

	/**
	 * Mark credits as used by updating referral payment status
	 *
	 * @param int   $user_id  User ID.
	 * @param float $amount   Amount to deduct.
	 * @param int   $order_id Order ID.
	 */
	protected function mark_credits_as_used( $user_id, $amount, $order_id ) {
		global $wpdb;

		// Get approved referrals ordered by time (oldest first).
		$referrals = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT id, commission FROM {$wpdb->prefix}wca_referrals WHERE affiliate = %d AND payment_status = 'approved' ORDER BY time",
				$user_id
			)
		);

		$remaining_amount = $amount;

		foreach ( $referrals as $referral ) {
			if ( $remaining_amount <= 0 ) {
				break;
			}

			$commission = (float) $referral->commission;

			if ( $commission <= $remaining_amount ) {
				// Consume this referral in full.
				$wpdb->update(
					$wpdb->prefix . 'wca_referrals',
					array(
						'payment_status' => 'paid',
						'transaction_id' => $order_id,
					),
					array( 'id' => $referral->id ),
					array( '%s', '%d' ),
					array( '%d' )
				);
				$remaining_amount -= $commission;
			} else {
				// Partial use: reduce the referral commission by the amount consumed.
				$wpdb->update(
					$wpdb->prefix . 'wca_referrals',
					array( 'commission' => $commission - $remaining_amount ),
					array( 'id' => $referral->id ),
					array( '%f' ),
					array( '%d' )
				);
				$remaining_amount = 0;
			}
		}
	}

	/**
	 * Updates payout data for an affiliate after a payout has been processed.
	 *
	 * Cleans up payout-related user meta data by removing the applied payout meta key
	 * for the specified affiliate.
	 *
	 * @since 3.0.0
	 *
	 * @param int   $affiliate_id The ID of the affiliate user.
	 * @param float $amount       The payout amount.
	 *
	 * @return void
	 */
	public function update_payout_data( int $affiliate_id, float $amount ): void {
		delete_user_meta( $affiliate_id, '_wc-affiliate-applied_payout' );
	}

	/**
	 * Saves notice dismissal data
	 *
	 * @since 3.0.0
	 *
	 * @param string $notice_type The name of the notice.
	 *
	 * @return void
	 */
	public function save_notice_dismissed_data( string $notice_type ) {
		update_option( $notice_type . '_notice_dismissed', true, false );
	}
}
