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

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

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

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

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

	public function is_active() {
		// Check if ConvesioPay is connected
		if ( ! PeachPay_ConvesioPay_Integration::connected() ) {
			return false;
		}
		
		// Check if BTCPay capability is enabled
		if ( ! PeachPay_ConvesioPay_Integration::is_capable('btcpay') ) {
			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_btcpay'] ?? 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() {
		// First register the ConvesioPay SDK
		wp_register_script(
			'convesiopay-sdk',
			'https://js.convesiopay.com/v1/',
			array(),
			'1.0.0',
			true
		);

		$script_path = '/core/payments/convesiopay/assets/js/btcpay-blocks.js';
		$script_asset_path = PEACHPAY_ABSPATH . 'core/payments/convesiopay/assets/js/btcpay-blocks.asset.php';
		$script_asset = file_exists( $script_asset_path )
			? require( $script_asset_path )
			: array(
				'dependencies' => array(
					'convesiopay-sdk',  // Add ConvesioPay SDK as dependency
					'wc-blocks-registry',
					'wc-settings',
					'wp-element',
					'wp-html-entities',
					'wp-i18n',
				),
				'version' => '2.6.0'
			);

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

		return [ 'wc-convesiopay-btcpay-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();
		if ( ! isset( $gateways['peachpay_convesiopay_btcpay'] ) || 'yes' !== $gateways['peachpay_convesiopay_btcpay']->enabled ) {
			return array();
		}
		$gateway = $gateways['peachpay_convesiopay_btcpay'];

		// Get ConvesioPay configuration (should be decrypted via PeachPay_ConvesioPay_Integration::config())
		$config = PeachPay_ConvesioPay_Integration::config();
		$integration_name = $config['integration_name'] ?? '';
		
		// Debug: Log the client key status
		$client_key = $config['client_key'] ?? '';
		$apiKey = $config['api_key'] ?? '';

		$icon_url = $gateway->get_icon_url( 'small', 'color' );
		$icon     = $icon_url ? '<img src="' . esc_url( $icon_url ) . '" alt="' . esc_attr( $gateway->get_title() ) . '" class="peachpay-payment-method-icon" />' : '';

		return [
			'enabled'          => true,
			'title'            => $gateway->get_title(),
			'description'      => $gateway->get_description(),
			'supports'         => $gateway->supports,
			'icon'             => $icon,
			'ajax_url'         => admin_url( 'admin-ajax.php' ),
			'nonce'            => wp_create_nonce( 'peachpay_convesiopay_nonce' ),
			'integration_name' => $integration_name,
			'convesiopay_config' => [
				'apiKey' => $apiKey,  // Should be decrypted by PeachPay_ConvesioPay_Integration::config()
				'clientKey' => $client_key,  // Should be decrypted by PeachPay_ConvesioPay_Integration::config()
				'api_url'   => peachpay_is_test_mode() ? 'https://api-qa.convesiopay.com' : 'https://api.convesiopay.com',
			],
		];
	}
}
