<?php
/**
 * Google Pay 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;
}

/**
 * Google_Pay_Block class.
 *
 * Handles Google Pay payment method registration for WooCommerce Blocks.
 */
class Google_Pay_Block extends AbstractPaymentMethodType {

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

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

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

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

	/**
	 * Get script handles for the payment method.
	 *
	 * @return array
	 */
	public function get_payment_method_script_handles() {
		// Use the same script handle as Card - 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();

		$total = '0.00';
		if ( WC()->cart ) {
			$total = WC()->cart->get_total( 'edit' );
		}

		$needs_shipping = WC()->cart ? WC()->cart->needs_shipping() : false;

		return array(
			'title'          => $this->settings['title'] ?? __( 'Google Pay', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			'description'    => $this->settings['description'] ?? '',
			'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,
			'shop_name'      => get_bloginfo( 'name' ),
			'total'          => $total,
			'button_color'   => $this->settings['button_color'] ?? 'black',
			'button_type'    => $this->settings['button_type'] ?? 'long',
			'needs_shipping' => $needs_shipping,
			'ajax_url'       => \WC_AJAX::get_endpoint( '%%endpoint%%' ),
			'nonce'          => wp_create_nonce( 'funnelkit-square-google-pay' ),
		);
	}
}
