<?php

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

/**
 * Cregis Payment Blocks Integration
 *
 * @since 1.0.0
 */
class Cregis_Blocks_Payment_Integration extends Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType {
    /**
     * Payment method name
     *
     * @var string
     */
    protected $name = 'cregispay';
    
    /**
     * Initialize the payment method
     */
    public function initialize() {
        $this->settings = get_option('cregis_payment_settings', array());
    }
    
    /**
     * Check if payment method is active
     *
     * @return bool
     */
    public function is_active() {
        // Check if the payment gateway is enabled
        $gateway_settings = get_option('woocommerce_cregispay_settings', array());
        $enabled = isset($gateway_settings['enabled']) ? $gateway_settings['enabled'] : 'no';
        
        // Check if API settings are configured
        $api_settings = get_option('cregis_payment_settings', array());
        $api_configured = !empty($api_settings['project_id']) && !empty($api_settings['secret_key']);
        
        return $enabled === 'yes' && $api_configured;
    }
    
    /**
     * Get payment method script handles
     *
     * @return array
     */
    public function get_payment_method_script_handles() {
        $script_url = CREGIS_PAYMENT_PLUGIN_URL . 'assets/js/blocks-payment.js';
        $version = CREGIS_PAYMENT_VERSION . '.' . time(); // Add timestamp to force cache refresh
        
        wp_register_script(
            'cregis-blocks-payment',
            $script_url,
            array('wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'),
            $version,
            true
        );
        
        // Load JavaScript translations
        wp_set_script_translations(
            'cregis-blocks-payment',
            'cregis-payment-for-woocommerce',
            CREGIS_PAYMENT_PLUGIN_DIR . 'languages'
        );
        
        return array('cregis-blocks-payment');
    }
    
    /**
     * Get payment method data
     *
     * @return array
     */
    public function get_payment_method_data() {
        $gateway_settings = get_option('woocommerce_cregispay_settings', array());
        $title = isset($gateway_settings['title']) ? $gateway_settings['title'] : __('Cryptocurrency Payment', 'cregis-payment-for-woocommerce');
        $description = isset($gateway_settings['description']) ? $gateway_settings['description'] : __('Pay with cryptocurrencies using Cregis', 'cregis-payment-for-woocommerce');
        
        return array(
            'title' => $title,
            'description' => $description,
            'supports' => array(
                'products'
            ),
            'isActive' => $this->is_active()
        );
    }
}
