<?php
/**
 * Gift Card 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;
}

/**
 * Gift_Card_Block class.
 *
 * Handles Gift Card payment method registration for WooCommerce Blocks.
 */
class Gift_Card_Block extends AbstractPaymentMethodType {

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

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

	/**
	 * Initialize the block.
	 *
	 * @return void
	 */
	public function initialize() {
		$this->settings = get_option( 'woocommerce_fkwcsq_gift_card_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 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();

		$data = array(
			'title'          => $this->settings['title'] ?? __( 'Gift Card', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			'description'    => $this->settings['description'] ?? __( 'Pay with a Square Gift Card', '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(),
			'i18n'           => array(
				'applying'        => __( 'Applying...', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'remove'          => __( 'Remove', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'success_full'    => __( 'Gift card applied successfully!', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				/* translators: %s: Remaining balance amount */
				'success_partial' => __( 'Gift card applied. Remaining balance: %s', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'error_invalid'   => __( 'Invalid gift card number.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'error_generic'   => __( 'An error occurred. Please try again.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			),
			'ajax_url'       => admin_url( 'admin-ajax.php' ),
			'apply_nonce'    => wp_create_nonce( 'fkwcsq_apply_gift_card' ),
			'remove_nonce'   => wp_create_nonce( 'fkwcsq_remove_gift_card' ),
		);

		// Add test gift card info when in sandbox mode.
		if ( Square::is_sandbox_mode() ) {
			$data['test_gift_card_info'] = array(
				'label'  => __( 'Test Gift Card Number:', 'funnelkit-payment-gateway-square-for-woocommerce' ),
				'number' => '7783 3200 0000 0000',
			);
		}

		return $data;
	}
}
