<?php
/**
 * WooCommerce Blocks Support for Red Envelope Payment
 */

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

defined('ABSPATH') || exit;

final class MRE_Blocks_Support extends AbstractPaymentMethodType {
    
    /**
     * Payment method name/id/slug
     */
    protected $name = 'red_envelope';
    
    /**
     * Gateway instance
     */
    private $gateway;
    
    /**
     * Initializes the payment method
     */
    public function initialize() {
        $this->settings = get_option('woocommerce_red_envelope_settings', array());
        $gateways = WC()->payment_gateways->payment_gateways();
        $this->gateway = isset($gateways[$this->name]) ? $gateways[$this->name] : null;

        // Fallback: If gateway singleton was stale (network-activated plugin timing),
        // try instantiating the gateway directly so is_active() can succeed.
        if (!$this->gateway && class_exists('WC_Gateway_M2C_Red_Envelope')) {
            $this->gateway = new \WC_Gateway_M2C_Red_Envelope();
        }
    }
    
    /**
     * Returns if this payment method should be active.
     * Falls back to checking DB settings directly if gateway instance is unavailable,
     * since WC_Payment_Gateways singleton may not include our gateway yet when
     * woocommerce_blocks_loaded fires (network-activated plugin timing issue).
     */
    public function is_active() {
        if ($this->gateway) {
            return $this->gateway->is_available();
        }
        // Fallback: check settings directly from DB
        $enabled = $this->settings['enabled'] ?? 'no';
        return $enabled === 'yes';
    }
    
    /**
     * Returns an array of scripts/handles to be registered for this payment method
     */
    public function get_payment_method_script_handles() {
        $asset_path = MRE_PLUGIN_DIR . 'assets/js/blocks.asset.php';
        $version = MRE_VERSION;
        $dependencies = array();
        
        if (file_exists($asset_path)) {
            $asset = require $asset_path;
            $version = isset($asset['version']) ? $asset['version'] : $version;
            $dependencies = isset($asset['dependencies']) ? $asset['dependencies'] : $dependencies;
        }
        
        wp_register_script(
            'mre-blocks-integration',
            MRE_PLUGIN_URL . 'assets/js/blocks.js',
            $dependencies,
            $version,
            true
        );
        
        return array('mre-blocks-integration');
    }
    
    /**
     * Returns an array of data to be exposed to the payment method script
     */
    public function get_payment_method_data() {
        $description = $this->get_setting('description');
        if (empty($description)) {
            $reward_percent = $this->get_setting('reward_percent', '60');
            $description = sprintf(
                'Pay with USDC via your Red Envelope Wallet on AQY Chain and receive %s%% AQY Coins rewards!',
                $reward_percent
            );
        }
        
        return array(
            'title'       => $this->get_setting('title', 'Red Envelope Payment'),
            'description' => $description,
            'supports'    => $this->gateway ? array_filter($this->gateway->supports, array($this->gateway, 'supports')) : array('products'),
            'icon'        => MRE_PLUGIN_URL . 'assets/icon.svg',
        );
    }
}
