<?php

namespace Pixeldev\SquareWooSync\Payments\Blocks;

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

final class WC_SquareSync_CashApp_Gateway_Blocks_Support extends AbstractPaymentMethodType
{

  private $gateway;
  protected $name = 'squaresync_cashapp';
  public $page = null;

  public function __construct() {}

  public function initialize()
  {
    $gateways = WC()->payment_gateways->payment_gateways();
    $this->gateway  = $gateways[$this->name];
  }

  public function is_active()
  {
    return $this->gateway ? $this->gateway->is_available() : false;
  }

  public function get_payment_method_script_handles()
  {
    $asset_path   = SQUAREWOOSYNC_PATH . '/build/blocks/cashapp-gateway.asset.php';
    $version      = SQUAREWOOSYNC_VERSION;
    $dependencies = array();

    if (file_exists($asset_path)) {
      $asset        = require $asset_path;
      $version      = is_array($asset) && isset($asset['version']) ? $asset['version'] : $version;
      $dependencies = is_array($asset) && isset($asset['dependencies']) ? $asset['dependencies'] : $dependencies;
    }

    // Enqueue Square Web Payments SDK based on environment (only if not already loaded)
    // Check both possible handles that might be used by other gateways
    $sdk_loaded = wp_script_is('wc-square-payments-sdk', 'enqueued') ||
                  wp_script_is('wc-square-payments-sdk', 'registered') ||
                  wp_script_is('square-web-payments', 'enqueued') ||
                  wp_script_is('square-web-payments', 'registered');

    if (!$sdk_loaded) {
      $settings = get_option('square-woo-sync_settings', []);
      $is_sandbox = isset($settings['environment']) && $settings['environment'] == 'sandbox';
      $square_sdk_url = $is_sandbox
        ? 'https://sandbox.web.squarecdn.com/v1/square.js'
        : 'https://web.squarecdn.com/v1/square.js';

      // Use the same handle as the main gateway for consistency
      wp_enqueue_script('wc-square-payments-sdk', $square_sdk_url, array(), null, false);
    }

    wp_register_script(
      'squaresync-cashapp-block',
      SQUAREWOOSYNC_URL . '/build/blocks/cashapp-gateway.js',
      array_merge($dependencies, array('wc-square-payments-sdk')),
      $version,
      true
    );

    return array('squaresync-cashapp-block');
  }

  public function get_payment_method_data()
  {
    $settings = get_option('square-woo-sync_settings', []);

    // Ensure the gateway object is initialized and available
    if ($this->gateway) {
      $applicationId = isset($settings['environment']) && $settings['environment'] == 'sandbox'
        ? 'sandbox-sq0idb-LMyDEf_itd9_DZukD9RGCQ'
        : 'sq0idp-HD7Og08_uCPjxkaEgoG40Q';

      $locationId = isset($settings['location']) ? $settings['location'] : '';

      return array(
        'title'       => $this->gateway->get_option('title'),
        'description' => $this->gateway->get_option('description'),
        'supports'    => $this->gateway->supports,
        'applicationId' => $applicationId,
        'locationId' => $locationId,
        'redirectURL' => wc_get_checkout_url(),
        'payment_nonce' => wp_create_nonce('wc-square-cashapp-payment'),
      );
    }

    return array();
  }
}
