<?php
namespace WooNooW\Compat;

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

/**
 * Payment Channels Handler
 * 
 * Automatically detects and exposes payment gateway channels (e.g., bank accounts)
 * to the WooNooW admin SPA.
 * 
 * @since 1.0.0
 */
class PaymentChannels {
    
    /**
     * Initialize hooks
     */
    public static function init() {
        add_filter('woonoow/payment_gateway_channels', [__CLASS__, 'detect_bacs_accounts'], 10, 3);
        add_filter('woonoow/payment_gateway_channels', [__CLASS__, 'detect_custom_channels'], 20, 3);
    }
    
    /**
     * Detect BACS (Bank Transfer) accounts
     * 
     * WooCommerce BACS gateway supports multiple bank accounts.
     * This automatically exposes them as channels.
     */
    public static function detect_bacs_accounts( array $channels, string $gateway_id, $gateway ): array {
        if ( $gateway_id !== 'bacs' ) {
            return $channels;
        }
        
        // Get account details from gateway settings
        // Try multiple methods to get account details
        $accounts = [];
        
        // Method 1: get_option (standard WooCommerce method)
        if ( method_exists( $gateway, 'get_option' ) ) {
            $accounts = $gateway->get_option( 'account_details', [] );
        }
        
        // Method 2: Direct property access (fallback)
        if ( empty( $accounts ) && isset( $gateway->account_details ) ) {
            $accounts = $gateway->account_details;
        }
        
        // Method 3: Get from WordPress options directly (last resort)
        if ( empty( $accounts ) ) {
            $gateway_settings = get_option( 'woocommerce_bacs_settings', [] );
            if ( isset( $gateway_settings['account_details'] ) ) {
                $accounts = $gateway_settings['account_details'];
            }
        }
        
        if ( empty( $accounts ) || ! is_array( $accounts ) ) {
            return $channels;
        }
        
        // Convert accounts to channels
        foreach ( $accounts as $index => $account ) {
            if ( ! is_array( $account ) || empty( $account['account_name'] ) ) {
                continue;
            }
            
            $bank_name    = $account['bank_name'] ?? '';
            $account_name = $account['account_name'] ?? '';
            $account_num  = $account['account_number'] ?? '';
            
            // Build channel title
            $title = $bank_name ? "{$bank_name} - {$account_name}" : $account_name;
            if ( $account_num ) {
                $title .= " ({$account_num})";
            }
            
            $channels[] = [
                'id'    => 'bacs_' . sanitize_title( $account_name . '_' . $index ),
                'title' => $title,
                'meta'  => $account,
            ];
        }
        
        return $channels;
    }
    
    /**
     * Detect custom channels from other gateways
     * 
     * This is a placeholder for future gateway integrations.
     * Other plugins can hook into woonoow/payment_gateway_channels
     * to provide their own channel logic.
     */
    public static function detect_custom_channels( array $channels, string $gateway_id, $gateway ): array {
        /**
         * Hook for third-party payment gateway channel detection
         * 
         * Example for Stripe with multiple accounts:
         * 
         * add_filter('woonoow/payment_gateway_channels', function($channels, $gateway_id, $gateway) {
         *     if ($gateway_id === 'stripe') {
         *         $accounts = get_option('stripe_connected_accounts', []);
         *         foreach ($accounts as $account) {
         *             $channels[] = [
         *                 'id'    => 'stripe_' . $account['id'],
         *                 'title' => $account['name'],
         *                 'meta'  => $account,
         *             ];
         *         }
         *     }
         *     return $channels;
         * }, 30, 3);
         */
        
        return $channels;
    }
}
