<?php

/**
 * NMI functions.
 *
 * @package PeachPay
 */

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

/**
 * Check if NMI gateway is enabled.
 *
 * @return boolean
 */
function peachpay_nmi_is_enabled()
{
	if (! function_exists('WC') || ! WC()->payment_gateways) {
		return false;
	}

	$gateways = WC()->payment_gateways->payment_gateways();
	$gateway = $gateways['peachpay_nmi_card'] ?? null;
	if (! $gateway) {
		return false;
	}

	$enabled_option = $gateway->get_option('enabled', 'no');
	$enabled_property = $gateway->enabled ?? 'no';
	return $enabled_option === 'yes' || $enabled_property === 'yes';
}

/**
 * Check if NMI is connected (has valid configuration).
 *
 * @return boolean
 */
function peachpay_nmi_connected()
{
	// First check if the gateway is enabled
	if ( ! peachpay_nmi_is_enabled() ) {
		return false;
	}

	// Get gateway settings directly from WooCommerce
	$settings = get_option( 'woocommerce_peachpay_nmi_card_settings', array() );
	$active_api_tab = $settings['active_api_tab'] ?? 'collectjs';

	// Check if the required key for the active mode is present
	if ( $active_api_tab === 'collectjs' ) {
		// Collect.js mode - need public tokenization key
		return ! empty( $settings['public_tokenization_key'] );
	}

	// Direct Post mode - need private key
	return ! empty( $settings['private_key'] );
}

/**
 * Get list of active payment methods
 * 
 * @return array Array of active payment method keys ('card')
 */
function peachpay_nmi_get_active_payment_methods()
{
	$active_methods = array();
	$gateways = WC()->payment_gateways->payment_gateways();

	// Check Card
	if (PeachPay_NMI_Integration::is_capable('card')) {
		$card_gateway = $gateways['peachpay_nmi_card'] ?? null;
		if ($card_gateway) {
			// Check both get_option and the enabled property
			$enabled_option = $card_gateway->get_option('enabled', 'no');
			$enabled_property = $card_gateway->enabled ?? 'no';
			if ($enabled_option === 'yes' || $enabled_property === 'yes') {
				$active_methods[] = 'card';
			}
		}
	}

	return $active_methods;
}

/**
 * Adds the feature flag for enabling NMI gateways.
 *
 * @param array $feature_list The list of features.
 */
function peachpay_nmi_register_feature( $feature_list )
{
	$active_methods = peachpay_nmi_get_active_payment_methods();

	$feature_list['peachpay_nmi_gateways'] = array(
		'enabled'  => (bool) peachpay_nmi_is_enabled(),
		'metadata' => array(
			'api_url'        => PeachPay_NMI_Integration::get_api_endpoint(),
			'environment'    => peachpay_is_test_mode() ? 'test' : 'live',
			'nonce'          => wp_create_nonce( 'peachpay_nmi_nonce' ),
			'active_methods' => $active_methods,
		),
	);

	$feature_list['peachpay_nmi_card_gateway'] = array(
		'enabled'  => (bool) PeachPay_NMI_Integration::is_capable( 'card' ),
		'metadata' => array(
			'title' => __( 'Card', 'peachpay-for-woocommerce' ),
		),
	);

	return $feature_list;
}

/**
 * Get NMI feature flags for frontend.
 *
 * @return array
 */
function peachpay_nmi_feature_flags()
{
	return array(
		'peachpay_nmi_gateways' => peachpay_nmi_is_enabled(),
		'peachpay_nmi_card_gateway' => PeachPay_NMI_Integration::is_capable('card'),
	);
}

/**
 * Handle NMI admin actions.
 *
 * @param string $action The action to handle.
 */
function peachpay_nmi_handle_admin_actions($action)
{
	// Handle NMI-specific admin actions
	if (strpos($action, 'nmi') !== false) {
		// Add any NMI-specific admin action handling here
		return;
	}
}