<?php
/**
 * Cash App Payment Block
 *
 * @package FunnelKit_Square
 * @subpackage Blocks
 */

namespace FKWCSQ\Includes\Blocks;

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

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

/**
 * Cash_App_Block class.
 *
 * Handles Cash App payment method registration for WooCommerce Blocks.
 */
class Cash_App_Block extends AbstractPaymentMethodType {

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

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

	/**
	 * Supported countries.
	 *
	 * @var array
	 */
	private $supported_countries = array( 'US' );

	/**
	 * Supported currencies.
	 *
	 * @var array
	 */
	private $supported_currencies = array( 'USD' );

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

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

		// Check country restriction.
		$base_country = WC()->countries->get_base_country();
		if ( ! in_array( $base_country, $this->supported_countries, true ) ) {
			return false;
		}

		// Check currency restriction.
		$currency = get_woocommerce_currency();
		if ( ! in_array( $currency, $this->supported_currencies, true ) ) {
			return false;
		}

		return true;
	}

	/**
	 * Get script handles for the 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 to pass to JavaScript.
	 *
	 * @return array
	 */
	public function get_payment_method_data() {
		$api      = \FKWCSQ\Includes\API::get_instance();
		$currency = get_woocommerce_currency();
		$country  = WC()->countries->get_base_country();

		return array(
			'title'          => $this->settings['title'] ?? __( 'Cash App Pay', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			'description'    => $this->settings['description'] ?? __( 'Pay securely with Cash App.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			'supports'       => array( 'products' ),
			'application_id' => Square::get_application_id(),
			'location_id'    => $api->get_location_id(),
			'is_sandbox'     => Square::is_sandbox_mode(),
			'enabled'        => $this->is_active(),
			'country_code'   => $country,
			'currency_code'  => $currency,
			'checkout_url'   => wc_get_checkout_url(),
			'i18n'           => array(
				'error_generic' => __( 'An error occurred. Please try again.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'not_supported' => __( 'Cash App Pay is not available.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			),
		);
	}
}
