<?php

namespace DigitalApps\PaymentsForAdyen\Blocks;

defined('ABSPATH') || exit;

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;

/**
 * Adyen Card Payment Method Type for WooCommerce Blocks
 * 
 * This class extends AbstractPaymentMethodType as required by WooCommerce Blocks
 * to properly register the Adyen Card payment method with the Blocks system.
 */
class Adyen_Card_Payment_Method_Type extends AbstractPaymentMethodType
{
	/**
	 * Payment method settings
	 * @var array
	 */
	protected $settings = [];

	/**
	 * Payment method name/id/slug (matches the gateway ID)
	 *
	 * @var string
	 */
	protected $name = 'digitalapps_payments_for_adyen_card';

	/**
	 * Returns the name of the payment method.
	 */
	public function get_name()
	{
		return 'digitalapps_payments_for_adyen_card';
	}

	/**
	 * Initializes the payment method type.
	 *
	 * This is called during server-side initialization and is where we
	 * populate settings and prepare the payment method.
	 */
	public function initialize()
	{
		// Load settings from both Adyen plugin settings and WooCommerce gateway settings
		$this->settings = array_merge(
			get_option('digitalapps_payments_for_adyen_settings', []),
			get_option('woocommerce_digitalapps_payments_for_adyen_card_settings', [])
		);
	}

	/**
	 * Returns if this payment method should be active.
	 * 
	 * If false, the scripts will not be enqueued and the payment method
	 * will not be available in the Blocks checkout.
	 *
	 * @return boolean
	 */
	public function is_active()
	{
		return 'yes' === $this->get_setting('enabled', 'no')
			&& !empty($this->get_setting('apiKey', ''))
			&& !empty($this->get_setting('merchantAccount', ''))
			&& !empty($this->get_setting('clientKey', ''));
	}

	/**
	 * Returns an array of script handles to enqueue for this payment method.
	 * 
	 * These scripts will be loaded as dependencies of the checkout block.
	 *
	 * @return string[]
	 */
	public function get_payment_method_script_handles()
	{
		$handle = 'digitalapps-payments-for-adyen-card-blocks';
		$asset_path = DIGITALAPPS_PAYMENTS_FOR_ADYEN_PATH . 'build/blocks/card.asset.php';
		$asset = file_exists($asset_path)
			? require $asset_path
			: ['dependencies' => [], 'version' => DIGITALAPPS_PAYMENTS_FOR_ADYEN_VERSION];
		$dependencies = array_unique(array_merge(
			$asset['dependencies'],
			['wp-element', 'wp-html-entities', 'wp-i18n', 'wc-blocks-registry', 'wc-settings']
		));

		wp_register_script(
			$handle,
			DIGITALAPPS_PAYMENTS_FOR_ADYEN_URL . 'build/blocks/card.js',
			$dependencies,
			$asset['version'],
			true
		);

		$style_path = DIGITALAPPS_PAYMENTS_FOR_ADYEN_PATH . 'build/blocks/card.css';
		if (file_exists($style_path)) {
			wp_enqueue_style(
				$handle,
				DIGITALAPPS_PAYMENTS_FOR_ADYEN_URL . 'build/blocks/card.css',
				[],
				$asset['version']
			);
			wp_style_add_data($handle, 'rtl', 'replace');
		}

		return [$handle];
	}

	/**
	 * Returns an array of script handles to enqueue in the admin context.
	 *
	 * @return string[]
	 */
	public function get_payment_method_script_handles_for_admin()
	{
		return $this->get_payment_method_script_handles();
	}

	/**
	 * Helper method to get settings from both sources
	 */
	protected function get_setting($key, $default = null)
	{
		// First check the merged settings from initialize()
		if (isset($this->settings[$key])) {
			return $this->settings[$key];
		}

		// Fallback to direct option lookup
		$digitalapps_payments_for_adyen_settings = get_option('digitalapps_payments_for_adyen_settings', []);
		if (isset($digitalapps_payments_for_adyen_settings[$key])) {
			return $digitalapps_payments_for_adyen_settings[$key];
		}

		$gateway_settings = get_option('woocommerce_digitalapps_payments_for_adyen_card_settings', []);
		if (isset($gateway_settings[$key])) {
			return $gateway_settings[$key];
		}

		return $default;
	}

	/**
	 * Returns an array of key=>value pairs of data made available to the payment method script.
	 *
	 * This data will be available client-side via wc.wcSettings.getSetting('digitalapps_payments_for_adyen_card_data').
	 *
	 * @return array
	 */
	public function get_payment_method_data()
	{
		$title = $this->get_setting('title', __('Credit/Debit Card (Adyen)', 'digital-apps-payments-for-adyen'));
		$description = $this->get_setting('description', '');
		$client_key = $this->get_setting('clientKey', '');
		$test_mode = $this->get_setting('testMode', true);
		$base_location = wc_get_base_location();
		$locale = str_replace('_', '-', determine_locale());

		$data = [
			'title' => $title,
			'description' => $description,
			'hasClientKey' => !empty($client_key),
			'clientKey' => $client_key,
			'environment' => $test_mode ? 'test' : 'live',
			'countryCode' => $base_location['country'] ?? 'US',
			'locale' => $locale,
			'supports' => [
				'features' => ['products', 'refunds'],
				'showSavedCards' => false,
				'showSaveOption' => false,
			],
		];

		return $data;
	}
}
