import {test} from 'node:test'
import assert from 'node:assert'
import {getWalletBalanceImpl} from '../../src/plugins/custom/privy.js'
import {Ticker} from '../../src/generated/ManagingApiTypes.js'

test('getWalletBalanceImpl should fetch wallet balance for valid address', async () => {
  const testWalletId = 'cm7vxs99f0007blcl8cmzv74t'
  // Note: Replace with actual wallet address associated with the wallet ID from Privy dashboard
  const testAddress = '0x932167388dD9aad41149b3cA23eBD489E2E2DD78'
  const assets = [Ticker.USDC, Ticker.ETH] // Required: array of assets using Ticker enum
  const chains = ['arbitrum', 'ethereum'] // Required: array of chains
  
  try {
    const balances = await getWalletBalanceImpl(testAddress, assets, chains)
    
    // Verify the response structure
    assert.ok(Array.isArray(balances), 'Should return an array of balances')
    
    // If balances exist, verify their structure
    if (balances.length > 0) {
      const balance = balances[0]
      
      // Verify Balance interface properties (all optional)
      if (balance.tokenName !== undefined) {
        assert.equal(typeof balance.tokenName, 'string', 'tokenName should be a string')
      }
      
      if (balance.amount !== undefined) {
        assert.equal(typeof balance.amount, 'number', 'amount should be a number')
      }
      
      if (balance.tokenAdress !== undefined) {
        assert.equal(typeof balance.tokenAdress, 'string', 'tokenAdress should be a string')
      }
      
      if (balance.chain !== undefined && balance.chain !== null) {
        assert.equal(typeof balance.chain, 'object', 'chain should be an object')
        
        if (balance.chain.chainId !== undefined) {
          // Chain ID should be either 42161 (Arbitrum) or 1 (Ethereum)
          const validChainIds = [42161, 1];
          assert.ok(validChainIds.includes(balance.chain.chainId), `chain.chainId should be one of ${validChainIds.join(', ')}, got ${balance.chain.chainId}`)
        }
        
        if (balance.chain.name !== undefined) {
          assert.equal(typeof balance.chain.name, 'string', 'chain.name should be a string')
        }
      }
      
      console.log(`✓ Found ${balances.length} balance(s) for wallet ${testWalletId}`)
      console.log('Sample balance:', JSON.stringify(balances[0], null, 2))
    } else {
      console.log(`✓ No balances found for wallet ${testWalletId} on chains ${chains.join(', ')}`)
    }
    
  } catch (error) {
    // If this is a real integration test, we might expect certain errors
    console.log('Error details:', error.message)
    
    // Common expected errors during testing:
    if (error.message.includes('User not found for wallet address')) {
      console.log('✓ Expected error: Wallet address not found in Privy (use real address from dashboard)')
    } else if (error.message.includes('Failed to get wallet balance')) {
      console.log('✓ Expected error: API call failed (check credentials and network)')
    } else {
      // Re-throw unexpected errors
      throw error
    }
  }
})