<?php
/**
 * Afterpay/Clearpay Block
 *
 * WooCommerce Blocks integration for Afterpay/Clearpay payments.
 *
 * @package FunnelKit_Square
 * @subpackage Blocks
 */

namespace FKWCSQ\Includes\Blocks;

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

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

/**
 * Afterpay_Block class.
 *
 * Registers Afterpay/Clearpay payment method for WooCommerce Block Checkout.
 */
class Afterpay_Block extends AbstractPaymentMethodType {

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

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

	/**
	 * Supported countries.
	 *
	 * @var array
	 */
	private $supported_countries = array( 'US', 'CA', 'AU', 'NZ', 'GB' );

	/**
	 * Supported currencies.
	 *
	 * @var array
	 */
	private $supported_currencies = array( 'USD', 'CAD', 'AUD', 'NZD', 'GBP' );

	/**
	 * Order limits by currency.
	 *
	 * @var array
	 */
	private $order_limits = array(
		'USD' => array(
			'min' => 1,
			'max' => 4000,
		),
		'CAD' => array(
			'min' => 1,
			'max' => 4000,
		),
		'AUD' => array(
			'min' => 1,
			'max' => 2000,
		),
		'NZD' => array(
			'min' => 1,
			'max' => 2000,
		),
		'GBP' => array(
			'min' => 1,
			'max' => 1200,
		),
	);

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

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

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

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

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

		return true;
	}

	/**
	 * Check if store uses Clearpay branding (UK only).
	 *
	 * @return bool
	 */
	private function is_clearpay() {
		return 'GB' === WC()->countries->get_base_country();
	}

	/**
	 * 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();
		$currency = get_woocommerce_currency();
		$limits   = isset( $this->order_limits[ $currency ] ) ? $this->order_limits[ $currency ] : array(
			'min' => 1,
			'max' => 4000,
		);

		$brand = $this->is_clearpay() ? 'Clearpay' : 'Afterpay';

		return array(
			'title'          => $this->settings['title'] ?? $brand,
			'description'    => $this->settings['description'] ?? __( 'Pay in 4 interest-free installments.', '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(),
			'country_code'   => WC()->countries->get_base_country(),
			'currency_code'  => $currency,
			'is_clearpay'    => $this->is_clearpay(),
			'show_messaging' => ( $this->settings['show_messaging'] ?? 'yes' ) === 'yes',
			'button_theme'   => $this->settings['button_theme'] ?? 'black_on_mint',
			'button_type'    => $this->settings['button_type'] ?? 'buy_now_with_afterpay',
			'min_amount'     => $limits['min'],
			'max_amount'     => $limits['max'],
			'enabled'        => $this->is_active(),
			'i18n'           => array(
				'error_generic'    => __( 'An error occurred. Please try again.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'not_supported'    => sprintf(
					/* translators: %s: brand name */
					__( '%s is not available in your region.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
					$brand
				),
				'amount_too_low'   => sprintf(
					/* translators: %s: minimum amount */
					__( 'Order total must be at least %s.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
					wc_price( $limits['min'] )
				),
				'amount_too_high'  => sprintf(
					/* translators: %s: maximum amount */
					__( 'Order total must be less than %s.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
					wc_price( $limits['max'] )
				),
				'installment_info' => __( 'Pay in 4 interest-free installments', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			),
		);
	}
}
