import { describe, expect, it } from 'vitest'
import {
  normalizeExplorerStats,
  normalizeTransactionTrend,
  summarizeRecentTransactions,
} from './stats'

describe('normalizeExplorerStats', () => {
  it('normalizes the local explorer stats shape', () => {
    expect(
      normalizeExplorerStats({
        total_blocks: 12,
        total_transactions: 34,
        total_addresses: 56,
        latest_block: 78,
      })
    ).toEqual({
      total_blocks: 12,
      total_transactions: 34,
      total_addresses: 56,
      latest_block: 78,
      average_block_time_ms: null,
      average_gas_price_gwei: null,
      network_utilization_percentage: null,
      transactions_today: null,
      freshness: null,
      completeness: null,
      sampling: null,
      diagnostics: null,
    })
  })

  it('normalizes the public Blockscout stats shape with string counts and no latest block', () => {
    expect(
      normalizeExplorerStats({
        total_blocks: '2784760',
        total_transactions: '15788',
        total_addresses: '376',
      })
    ).toEqual({
      total_blocks: 2784760,
      total_transactions: 15788,
      total_addresses: 376,
      latest_block: null,
      average_block_time_ms: null,
      average_gas_price_gwei: null,
      network_utilization_percentage: null,
      transactions_today: null,
      freshness: null,
      completeness: null,
      sampling: null,
      diagnostics: null,
    })
  })

  it('normalizes freshness and completeness metadata when present', () => {
    expect(
      normalizeExplorerStats({
        total_blocks: '1',
        total_transactions: '2',
        total_addresses: '3',
        latest_block: '4',
        freshness: {
          chain_head: { block_number: '4', timestamp: '2026-04-10T22:10:15Z', age_seconds: '1', source: 'reported' },
          latest_indexed_block: { block_number: '4', timestamp: '2026-04-10T22:10:15Z', age_seconds: '1' },
          latest_indexed_transaction: { block_number: '3', timestamp: '2026-04-10T22:00:15Z', age_seconds: '600' },
          latest_non_empty_block: { block_number: '3', timestamp: '2026-04-10T22:00:15Z', age_seconds: '600', distance_from_head: '1' },
        },
        completeness: {
          transactions_feed: 'partial',
          blocks_feed: 'complete',
        },
        sampling: {
          stats_generated_at: '2026-04-10T22:10:16Z',
        },
      }),
    ).toMatchObject({
      freshness: {
        chain_head: { block_number: 4, age_seconds: 1, source: 'reported' },
        latest_non_empty_block: { distance_from_head: 1 },
      },
      completeness: {
        transactions_feed: 'partial',
        blocks_feed: 'complete',
      },
      sampling: {
        stats_generated_at: '2026-04-10T22:10:16Z',
      },
    })
  })

  it('normalizes transaction trend chart data', () => {
    expect(
      normalizeTransactionTrend({
        chart_data: [
          { date: '2026-04-08', transaction_count: '2' },
          { date: '2026-04-07', transaction_count: 101 },
        ],
      }),
    ).toEqual([
      { date: '2026-04-08', transaction_count: 2 },
      { date: '2026-04-07', transaction_count: 101 },
    ])
  })

  it('summarizes recent activity metrics from main-page transactions', () => {
    expect(
      summarizeRecentTransactions([
        {
          status: 'ok',
          transaction_types: ['contract_call', 'token_transfer'],
          gas_used: '100',
          fee: { value: '200' },
        },
        {
          status: 'error',
          transaction_types: ['contract_creation'],
          gas_used: '300',
          fee: { value: '400' },
        },
      ]),
    ).toEqual({
      success_rate: 0.5,
      failure_rate: 0.5,
      average_gas_used: 200,
      average_fee_wei: 300,
      contract_creations: 1,
      token_transfer_txs: 1,
      contract_calls: 1,
      contract_creation_share: 0.5,
      token_transfer_share: 0.5,
      contract_call_share: 0.5,
      sample_size: 2,
    })
  })
})
