<?php
/**
 * Payouts Service Module — frontend-safe registration.
 *
 * Houses the bits of the Payouts Service that need to register on every request
 * (front-end, WP-CLI, cron, Action Scheduler), not only inside the admin. The
 * full admin-only class still lives in
 * `includes/admin/payouts/class-payouts-service.php` and handles notices,
 * connect/reconnect/disconnect actions, the preview-payout fee display, and
 * the per-payout process handler.
 *
 * History: the registration filter used to live in the admin class, which was
 * loaded only when `is_admin()`. That left the affiliate area, the Affiliate
 * Portal, WP-CLI, and async batch workers seeing a payout-methods registry
 * that didn't include 'payouts-service'. The Portal addon worked around it
 * with an unconditional re-registration in its compat layer; that compat is
 * no longer needed once this file runs on every context.
 *
 * @package    AffiliateWP
 * @subpackage Payouts/Methods/PayoutsService
 * @since      2.33.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

require_once __DIR__ . '/functions.php';

/**
 * Registers 'Payouts Service' in the payout methods registry when the admin has
 * enabled it site-wide and the service hasn't been sunset.
 *
 * @since 2.33.0
 *
 * @param array<string,string> $payout_methods Existing registry entries.
 * @return array<string,string>
 */
function affwp_payouts_service_register_payout_method( $payout_methods ) {

	if ( function_exists( 'affwp_is_payouts_service_sunset_passed' ) && affwp_is_payouts_service_sunset_passed() ) {
		return $payout_methods;
	}

	if ( ! affiliate_wp()->settings->get( 'enable_payouts_service', false ) ) {
		return $payout_methods;
	}

	$payout_methods['payouts-service'] = __( 'Payouts Service', 'affiliate-wp' );

	return $payout_methods;
}
add_filter( 'affwp_payout_methods', 'affwp_payouts_service_register_payout_method', 100 );

/**
 * Filters Payouts Service's enabled state. Returns false when the toggle is off,
 * when the connection isn't active, or when credentials are missing — so the
 * admin Settings → Payouts toggle is authoritative regardless of where the
 * method got registered.
 *
 * @since 2.33.0
 *
 * @param bool   $enabled       Current enabled state from upstream filters.
 * @param string $payout_method Method identifier being checked.
 * @return bool
 */
function affwp_payouts_service_is_enabled( $enabled, $payout_method ) {

	if ( 'payouts-service' !== $payout_method ) {
		return $enabled;
	}

	if ( ! affiliate_wp()->settings->get( 'enable_payouts_service', false ) ) {
		return false;
	}

	$vendor_id         = affiliate_wp()->settings->get( 'payouts_service_vendor_id', 0 );
	$access_key        = affiliate_wp()->settings->get( 'payouts_service_access_key', '' );
	$connection_status = affiliate_wp()->settings->get( 'payouts_service_connection_status', '' );

	if ( 'active' !== $connection_status || ! ( $vendor_id && $access_key ) ) {
		return false;
	}

	return $enabled;
}
add_filter( 'affwp_is_payout_method_enabled', 'affwp_payouts_service_is_enabled', 10, 2 );

add_filter( 'affwp_batch_payout_initial_status', 'affwp_payouts_service_filter_batch_initial_status', 10, 2 );
add_action( 'affwp_batch_generate_payouts_completed', 'affwp_payouts_service_process_completed_batch' );
