<?php
/**
 * Affiliate Management Functions
 *
 * Core functions for managing affiliate status, validation, and basic affiliate operations.
 * This includes checking affiliate status, retrieving affiliate information, and managing
 * affiliate lifecycle states.
 *
 * @since   3.0.0
 * @package WC_Affiliate
 */

use WC_Affiliate\Models\Affiliate;

/**
 * Check if a user is an affiliate.
 *
 * @since 1.0.0
 *
 * @param string $user_id User ID. Defaults to current user.
 *
 * @return bool True if user is an affiliate, false otherwise.
 */
function wca_is_affiliate( string $user_id = '' ): bool {
	if ( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	$affiliate = new Affiliate( $user_id );

	// Allow filtering affiliate status.
	return apply_filters( 'wca_is_affiliate', $affiliate->exists(), $user_id );
}

/**
 * Get affiliate status for a user.
 *
 * @since 1.0.0
 *
 * @param int|string $user_id User ID. Defaults to current user.
 *
 * @return string Affiliate status.
 */
function wca_get_affiliate_status( $user_id = '' ): string {
	if ( ! is_user_logged_in() ) {
		return 'pending';
	}

	if ( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	$user_model = new \WC_Affiliate\Models\User( $user_id );

	$status = $user_model->get_meta( '_wc_affiliate_status' );

	// Allow filtering affiliate status.
	return apply_filters( 'wca_get_affiliate_status', $status, $user_id );
}

/**
 * Check if user is an active affiliate.
 *
 * @since 1.0.0
 *
 * @param int|string $user_id User ID. Defaults to current user.
 *
 * @return bool True if user is active affiliate.
 */
function wca_is_active_affiliate( $user_id = '' ): bool {
	if ( ! is_user_logged_in() ) {
		return false;
	}

	if ( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	return wca_get_affiliate_status( $user_id ) === 'active';
}

/**
 * Affiliate statuses
 *
 * @since 1.0
 * @return array
 */
function wca_get_affiliate_statuses(): array {
	return apply_filters(
		'wc-affiliate-affiliate_statuses',
		array(
			'pending'   => __( 'Pending', 'wc-affiliate' ),
			'active'    => __( 'Active', 'wc-affiliate' ),
			'rejected'  => __( 'Rejected', 'wc-affiliate' ),
			'suspended' => __( 'Suspended', 'wc-affiliate' ),
		)
	);
}

/**
 * Check if user's email is verified for affiliate program
 *
 * @param int $user_id User ID to check (optional, defaults to current user).
 *
 * @return bool
 */
function wca_is_mail_verified( int $user_id = 0 ): bool {
	$user_id = $user_id ? $user_id : get_current_user_id();
	if ( ! $user_id ) {
		return false;
	}

	// Use User model for consistent meta access.
	$user_model = new \WC_Affiliate\Models\User( $user_id );
	$applied    = $user_model->get_meta( '_wc_affiliate_time_applied' );
	$enabled    = wca_is_email_validation_enabled();

	if ( ! $enabled || ! $applied ) {
		return true;
	}

	$status           = $user_model->get_meta( '_wc_affiliate_status' );
	$promotion_method = $user_model->get_meta( '_wc_affiliate_promotion_method' );

	if ( ( ! $status && $promotion_method ) || ( ! $status && ! $promotion_method ) ) {
		return false;
	}

	return true;
}
