<?php

namespace Ahapaybuynowpaylater\Blocks;

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

/**
 * Class Ahapaybuynowpaylater_WC_Blocks_Support
 *
 * This class integrates the AhaPay payment gateway with WooCommerce Blocks.
 * It provides the necessary methods to register the payment method and load its scripts.
 */
class Ahapaybuynowpaylater_WC_Blocks_Support extends AbstractPaymentMethodType {
	private $gateway;
	protected $name = 'ahapaybuynowpaylater';
	protected $settings = [];

	/**
	 * Constructor for the AhaPay Blocks Support class.
	 */
	public function initialize() {
		// get payment gateway settings
		$this->settings = get_option("woocommerce_{$this->name}_settings", array());
		// you can also initialize your payment gateway here
		 $gateways = WC()->payment_gateways->payment_gateways();
		 $this->gateway  = $gateways[ $this->name ];
	}

	/*
	 * Check if the payment method is active.
	 * */
	public function is_active() {
		return ! empty( $this->settings[ 'enabled' ] ) && 'yes' === $this->settings[ 'enabled' ];
	}

	/**
	 * Returns an array of script handles to enqueue for this payment method in
	 * the frontend context.
	 *
	 * @return string[]
	 */
	public function get_payment_method_script_handles() {
		if (!$this->is_checkout_block()) {
			return []; // Don't load JS outside of block checkout
		}

		if (WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout')) {
			if (is_order_received_page()) {
				return [];
			}
		}

		$asset_path = AHAPAYBUYNOWPAYLATER_PLUGIN_DIR . 'assets/blocks/index.asset.php';
		$version = time(); // Default fallback version
		$dependencies = ['wp-element', 'wp-html-entities'];

		if (file_exists($asset_path)) {
			$asset = require $asset_path;
			$version = isset($asset['version']) ? $asset['version'] : $version;
			$dependencies = isset($asset['dependencies']) ? $asset['dependencies'] : $dependencies;
		}

		// Add essential Block dependencies
		$dependencies = array_merge($dependencies, [
			'wc-blocks-registry',
			'wc-settings',
			'wp-element',
			'wp-components',
			'wp-blocks',
			'wp-i18n'
		]);

		// Register canonical ahapaybuynowpaylater-block handle (if not already registered)
		if (!wp_script_is('ahapaybuynowpaylater-block', 'registered')) {
			wp_register_script(
				'ahapaybuynowpaylater-block',
				AHAPAYBUYNOWPAYLATER_PLUGIN_URL . 'assets/blocks/index.js',
				array_unique($dependencies),
				$version,
				true
			);


		}

		return ['ahapaybuynowpaylater-block'];
	}

	/**
	 * Returns the payment method data for the AhaPay payment gateway.
	 *
	 * @return array
	 */
	public function get_payment_method_data()
	{
		return array(
			'title' => isset( $this->settings['title'] ) ? $this->settings['title'] : 'AhaPay',
			'description' => isset( $this->settings['description'] ) ? $this->settings['description'] : 'AhaPay \u2013\u00a04 easy payments, 0% interest rate',
			'supports' => ['products', 'refunds'], // Add the features your gateway supports
			'logoUrl' => AHAPAYBUYNOWPAYLATER_PLUGIN_URL .'assets/img/ahapay-logo.png',
		);
	}

	/**
	 * check if the checkout block is being used on the checkout page.
	 *
	 * @return string[]
	 */
	private function is_checkout_block()
	{
		return WC_Blocks_Utils::has_block_in_page( wc_get_page_id('checkout'), 'woocommerce/checkout' );

	}
}
