<?php
/**
 * ConvesioPay Blocks Support Class
 *
 * @package PeachPay
 */

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

/**
 * ConvesioPay Blocks Support Class
 */
class ConvesioPay_Blocks_Support extends \Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType {

	/**
	 * Payment method name
	 */
	protected $name = 'peachpay_convesiopay_card';

	/**
	 * Initialize the payment method type
	 */
	public function initialize() {
		$this->settings = get_option( 'woocommerce_peachpay_convesiopay_card_settings', [] );
	}

	/**
	 * Returns if this payment method should be active
	 */
	public function is_active() {
		// Check if ConvesioPay is connected
		if ( ! PeachPay_ConvesioPay_Integration::connected() ) {
			return false;
		}
		
		// Check if card capability is enabled
		if ( ! PeachPay_ConvesioPay_Integration::is_capable('card') ) {
			return false;
		}
		
		// Check if gateway exists and is enabled (but don't call is_available() due to hardcoded return false)
		$all_gateways = WC()->payment_gateways()->payment_gateways();
		$gateway = $all_gateways['peachpay_convesiopay_card'] ?? null;
		
		if ( ! $gateway || $gateway->enabled !== 'yes' ) {
			return false;
		}
		
		return true;
	}

	/**
	 * Returns an array of scripts/handles to be registered for this payment method
	 */
	public function get_payment_method_script_handles() {
		$script_path = '/core/payments/convesiopay/assets/js/blocks.js';
		$script_asset_path = PEACHPAY_ABSPATH . 'core/payments/convesiopay/assets/js/blocks.asset.php';
		$script_asset = file_exists( $script_asset_path )
			? require( $script_asset_path )
			: array(
				'dependencies' => array(
					'wc-blocks-registry',
					'wc-settings',
					'wp-element',
					'wp-html-entities',
					'wp-i18n',
				),
				'version' => '1.2.0'
			);

		wp_register_script(
			'wc-convesiopay-blocks-integration',
			plugins_url( 'peachpay-for-woocommerce' . $script_path ),
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		return [ 'wc-convesiopay-blocks-integration' ];
	}

	/**
	 * Returns an array of key=>value pairs of data made available to the payment methods script.
	 * Uses payment_gateways() only to avoid get_available_payment_gateways() (which triggers is_available() for all gateways).
	 */
	public function get_payment_method_data() {
		$gateways = WC()->payment_gateways()->payment_gateways();
		$gateway  = ( isset( $gateways['peachpay_convesiopay_card'] ) && 'yes' === $gateways['peachpay_convesiopay_card']->enabled )
			? $gateways['peachpay_convesiopay_card']
			: null;

		// Add AJAX configuration for transaction creation
		$ajax_config = array(
			'ajax_url' => admin_url('admin-ajax.php'),
			'nonce' => wp_create_nonce('peachpay_convesiopay_nonce')
		);
		
		return [
			'title'       => $gateway ? $gateway->get_title() : __( 'ConvesioPay Card', 'peachpay-for-woocommerce' ),
			'description' => $gateway ? $gateway->get_description() : __( 'Pay securely with your credit or debit card via ConvesioPay.', 'peachpay-for-woocommerce' ),
			'supports'    => $gateway ? array_filter( $gateway->supports, [ $gateway, 'supports' ] ) : [],
			'is_blocks'   => true, // Flag to indicate this is blocks implementation
			'ajax_config' => $ajax_config, // AJAX configuration for transaction creation
		];
	}
}