import { describe, expect, it } from 'vitest'

import {
  normalizeAddressInfo,
  normalizeAddressTokenBalance,
  normalizeAddressTokenTransfer,
  normalizeTransaction,
} from './blockscout'

describe('blockscout normalization helpers', () => {
  it('normalizes richer transaction details including decoded input and token transfers', () => {
    const transaction = normalizeTransaction({
      hash: '0xabc',
      block_number: 10,
      from: { hash: '0xfrom' },
      to: { hash: '0xto' },
      value: '1000000000000000000',
      gas_limit: 21000,
      gas_used: 21000,
      gas_price: 123,
      status: 'ok',
      timestamp: '2026-04-09T00:00:00.000000Z',
      method: '0xa9059cbb',
      revert_reason: null,
      transaction_tag: 'Transfer',
      fee: { value: '21000' },
      decoded_input: {
        method_call: 'transfer(address,uint256)',
        method_id: '0xa9059cbb',
        parameters: [{ name: 'to', type: 'address', value: '0xto' }],
      },
      token_transfers: [
        {
          from: { hash: '0xfrom' },
          to: { hash: '0xto' },
          token: {
            address: '0xtoken',
            symbol: 'TKN',
            name: 'Token',
            decimals: '6',
          },
          total: {
            decimals: '6',
            value: '5000000',
          },
        },
      ],
    }, 138)

    expect(transaction.method).toBe('0xa9059cbb')
    expect(transaction.transaction_tag).toBe('Transfer')
    expect(transaction.decoded_input?.method_call).toBe('transfer(address,uint256)')
    expect(transaction.token_transfers).toHaveLength(1)
    expect(transaction.token_transfers?.[0].token_symbol).toBe('TKN')
  })

  it('normalizes address balances and trust signals', () => {
    const info = normalizeAddressInfo({
      hash: '0xaddr',
      coin_balance: '123',
      is_contract: true,
      is_verified: true,
      has_token_transfers: true,
      has_tokens: true,
      creation_transaction_hash: '0xcreate',
      name: 'Treasury',
      token: {
        address: '0xtoken',
        symbol: 'cUSDT',
        name: 'Tether USD (Compliant)',
        decimals: '6',
        type: 'ERC-20',
        total_supply: '1000',
        holders: '10',
      },
      public_tags: [{ label: 'Core' }],
      private_tags: [],
      watchlist_names: ['Ops'],
    }, {
      transactions_count: '4',
      token_balances_count: '2',
      token_transfers_count: '8',
      internal_transactions_count: '6',
      logs_count: '9',
    }, 138)

    expect(info.balance).toBe('123')
    expect(info.is_verified).toBe(true)
    expect(info.tags).toEqual(['Core', 'Ops'])
    expect(info.creation_transaction_hash).toBe('0xcreate')
    expect(info.token_contract?.symbol).toBe('cUSDT')
    expect(info.internal_transaction_count).toBe(6)
  })

  it('normalizes address token balances and transfers', () => {
    const balance = normalizeAddressTokenBalance({
      token: {
        address: '0xtoken',
        name: 'Stable',
        symbol: 'STBL',
        decimals: '6',
        holders: '11',
        total_supply: '1000000',
      },
      value: '1000',
    })
    const transfer = normalizeAddressTokenTransfer({
      transaction_hash: '0xtx',
      block_number: 9,
      from: { hash: '0xfrom', name: 'Sender' },
      to: { hash: '0xto', name: 'Receiver' },
      token: {
        address: '0xtoken',
        symbol: 'STBL',
        name: 'Stable',
        decimals: '6',
      },
      total: {
        decimals: '6',
        value: '1000',
      },
      timestamp: '2026-04-09T00:00:00.000000Z',
    })

    expect(balance.holder_count).toBe(11)
    expect(balance.token_symbol).toBe('STBL')
    expect(transfer.from_label).toBe('Sender')
    expect(transfer.to_label).toBe('Receiver')
    expect(transfer.value).toBe('1000')
  })
})
