<?php
/**
 * ACH Bank Transfer Block
 *
 * WooCommerce Blocks integration for ACH payments.
 *
 * @package FunnelKit_Square
 * @subpackage Blocks
 */

namespace FKWCSQ\Includes\Blocks;

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

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

/**
 * ACH_Block class.
 *
 * Registers ACH payment method for WooCommerce Block Checkout.
 */
class ACH_Block extends AbstractPaymentMethodType {

	/**
	 * Payment method name.
	 *
	 * @var string
	 */
	protected $name = 'fkwcsq_ach';

	/**
	 * Gateway settings.
	 *
	 * @var array
	 */
	protected $settings;

	/**
	 * Initialize the block.
	 *
	 * @return void
	 */
	public function initialize() {
		$this->settings = get_option( 'woocommerce_fkwcsq_ach_settings', array() );
	}

	/**
	 * Check if ACH is available.
	 *
	 * ACH is only available in the US with USD currency.
	 *
	 * @return bool
	 */
	public function is_active() {
		if ( ! isset( $this->settings['enabled'] ) || 'yes' !== $this->settings['enabled'] ) {
			return false;
		}

		// ACH is US only.
		$base_country = WC()->countries->get_base_country();
		if ( 'US' !== $base_country ) {
			return false;
		}

		// ACH is USD only.
		$currency = get_woocommerce_currency();
		if ( 'USD' !== $currency ) {
			return false;
		}

		// Check if Square is connected.
		if ( ! Square::get_application_id() ) {
			return false;
		}

		return true;
	}

	/**
	 * Get script handles for payment method.
	 *
	 * @return array
	 */
	public function get_payment_method_script_handles() {
		// Use the same script handle as other blocks - they share the same bundle.
		return array( 'fkwcsq-blocks-integration' );
	}

	/**
	 * Get payment method data for frontend.
	 *
	 * @return array
	 */
	public function get_payment_method_data() {
		$api = \FKWCSQ\Includes\API::get_instance();

		// Generate unique transaction ID for ACH session.
		$transaction_id = wp_generate_uuid4();
		if ( function_exists( 'WC' ) && WC()->session ) {
			WC()->session->set( 'fkwcsq_ach_transaction_id', $transaction_id );
		}

		return array(
			'title'          => $this->settings['title'] ?? __( 'Bank Transfer (ACH)', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			'description'    => $this->settings['description'] ?? __( 'Pay directly from your bank account. Payments typically take 2-3 business days to process.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			'supports'       => array( 'products', 'refunds' ),
			'application_id' => Square::get_application_id(),
			'location_id'    => $api->get_location_id(),
			'is_sandbox'     => Square::is_sandbox_mode(),
			'redirect_url'   => wc_get_checkout_url(),
			'transaction_id' => $transaction_id,
			'currency'       => get_woocommerce_currency(),
			'enabled'        => $this->is_active(),
			'i18n'           => array(
				'error_generic'   => __( 'An error occurred. Please try again.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'not_supported'   => __( 'ACH payments are not supported in your region.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'payment_pending' => __( 'Your payment is being processed. This may take 2-3 business days.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'account_holder'  => __( 'Account Holder Name', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'connect_bank'    => __( 'Connect Bank Account', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'bank_connected'  => __( 'Bank Connected', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'connecting'      => __( 'Connecting...', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'info_text'       => __( 'ACH bank transfers typically take 2-3 business days to process. Your order will be held until payment clears.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			),
		);
	}
}
