<?php
/**
 * Stablezact Wallet Settings Page
 *
 * Allows merchants to configure their wallet addresses for all supported networks
 * without leaving WordPress/WooCommerce
 *
 * @package Stablezact
 * @version 1.0.0
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

// Get gateway settings for API credentials
$stablezact_gateway_settings = get_option('woocommerce_stablezact_settings', array());
$stablezact_api_url = isset($stablezact_gateway_settings['api_url']) && !empty($stablezact_gateway_settings['api_url'])
    ? $stablezact_gateway_settings['api_url']
    : 'https://hub.stablezact.com/api';
$stablezact_public_key = isset($stablezact_gateway_settings['public_key']) ? $stablezact_gateway_settings['public_key'] : '';

// Supported networks configuration
$stablezact_networks = array(
    'ethereum' => array(
        'name' => 'Ethereum',
        'icon' => '⟠',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    ),
    'bsc' => array(
        'name' => 'BSC (Binance Smart Chain)',
        'icon' => '⬡',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    ),
    'polygon' => array(
        'name' => 'Polygon',
        'icon' => '⬢',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    ),
    'arbitrum' => array(
        'name' => 'Arbitrum',
        'icon' => '◆',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    ),
    'optimism' => array(
        'name' => 'Optimism',
        'icon' => '🔴',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    ),
    'avalanche' => array(
        'name' => 'Avalanche',
        'icon' => '❄️',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    ),
    'celo' => array(
        'name' => 'Celo',
        'icon' => '💚',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    ),
    'base' => array(
        'name' => 'Base',
        'icon' => '🔵',
        'type' => 'evm',
        'tokens' => array('USDT', 'USDC')
    )
);

// Enqueue styles and scripts
wp_enqueue_style(
    'stablezact-wallet-settings',
    plugin_dir_url(dirname(__FILE__)) . '../assets/css/wallet-settings.css',
    array(),
    '1.0.0'
);

wp_enqueue_script(
    'stablezact-wallet-settings',
    plugin_dir_url(dirname(__FILE__)) . '../assets/js/wallet-settings.js',
    array('jquery'),
    '1.0.0',
    true
);

// Localize script with API data
wp_localize_script('stablezact-wallet-settings', 'stablezactWalletSettings', array(
    'ajax_url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('stablezact_wallet_settings_nonce'),
    'api_url' => $stablezact_api_url,
    'public_key' => $stablezact_public_key,
    'networks' => $stablezact_networks
));
?>

<div class="wrap stablezact-settings-wrap">
    <h1><?php echo esc_html__('Stablezact Wallet Settings', 'stablezact-crypto-payments'); ?></h1>

    <div class="stablezact-settings-notice"></div>

    <div class="stablezact-settings-container">
        <div class="stablezact-settings-header">
            <h2><?php echo esc_html__('Receive Wallet Addresses', 'stablezact-crypto-payments'); ?></h2>
            <p class="description">
                <?php echo esc_html__('Configure the wallet addresses where you want to receive cryptocurrency payments on each network.', 'stablezact-crypto-payments'); ?>
            </p>
        </div>

        <div id="stablezact-loading" class="stablezact-loading">
            <div class="stablezact-spinner"></div>
            <p><?php echo esc_html__('Loading wallet addresses...', 'stablezact-crypto-payments'); ?></p>
        </div>

        <form id="stablezact-wallets-form" class="stablezact-wallets-form" style="display: none;">
            <div class="stablezact-wallets-grid">
                <?php foreach ($stablezact_networks as $stablezact_network_key => $stablezact_network): ?>
                    <div class="stablezact-wallet-card" data-network="<?php echo esc_attr($stablezact_network_key); ?>">
                        <div class="stablezact-wallet-header">
                            <span class="stablezact-network-icon"><?php echo esc_html($stablezact_network['icon']); ?></span>
                            <div class="stablezact-network-info">
                                <h3><?php echo esc_html($stablezact_network['name']); ?></h3>
                                <span class="stablezact-network-type"><?php echo esc_html(strtoupper($stablezact_network['type'])); ?></span>
                            </div>
                        </div>

                        <div class="stablezact-wallet-body">
                            <label for="wallet_<?php echo esc_attr($stablezact_network_key); ?>">
                                <?php echo esc_html__('Wallet Address', 'stablezact-crypto-payments'); ?>
                                <span class="required">*</span>
                            </label>

                            <input
                                type="text"
                                id="wallet_<?php echo esc_attr($stablezact_network_key); ?>"
                                name="wallets[<?php echo esc_attr($stablezact_network_key); ?>]"
                                class="stablezact-wallet-input"
                                placeholder="<?php echo esc_attr($stablezact_network['type'] === 'evm' ? '0x...' : 'Enter wallet address'); ?>"
                                data-network-type="<?php echo esc_attr($stablezact_network['type']); ?>"
                            />

                            <span class="stablezact-validation-message"></span>

                            <div class="stablezact-supported-tokens">
                                <strong><?php echo esc_html__('Supported Tokens:', 'stablezact-crypto-payments'); ?></strong>
                                <span><?php echo esc_html(implode(', ', $stablezact_network['tokens'])); ?></span>
                            </div>
                        </div>

                        <div class="stablezact-wallet-status">
                            <span class="status-icon"></span>
                            <span class="status-text"><?php echo esc_html__('Not configured', 'stablezact-crypto-payments'); ?></span>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>

            <div class="stablezact-form-actions">
                <button type="submit" class="button button-primary button-large" id="stablezact-save-wallets">
                    <span class="dashicons dashicons-saved"></span>
                    <?php echo esc_html__('Save All Wallet Addresses', 'stablezact-crypto-payments'); ?>
                </button>

                <button type="button" class="button button-secondary" id="stablezact-validate-all">
                    <span class="dashicons dashicons-yes-alt"></span>
                    <?php echo esc_html__('Validate All Addresses', 'stablezact-crypto-payments'); ?>
                </button>
            </div>
        </form>

        <div class="stablezact-settings-info">
            <h3><?php echo esc_html__('Important Information', 'stablezact-crypto-payments'); ?></h3>
            <ul>
                <li><?php echo esc_html__('⚠️ Double-check wallet addresses before saving. Incorrect addresses may result in permanent loss of funds.', 'stablezact-crypto-payments'); ?></li>
                <li><?php echo esc_html__('✅ All addresses are validated for correct format before saving.', 'stablezact-crypto-payments'); ?></li>
                <li><?php echo esc_html__('🔒 You must configure at least one network to accept crypto payments.', 'stablezact-crypto-payments'); ?></li>
                <li><?php echo esc_html__('💡 The same wallet address can be used across multiple EVM-compatible networks.', 'stablezact-crypto-payments'); ?></li>
                <li><?php echo esc_html__('⚡ Settings are synced with Stablezact servers automatically.', 'stablezact-crypto-payments'); ?></li>
            </ul>
        </div>
    </div>
</div>
