import { describe, expect, it } from 'vitest'
import {
  resolveEffectiveFreshness,
  resolveFreshnessSourceLabel,
  summarizeFreshnessConfidence,
} from './explorerFreshness'

describe('resolveEffectiveFreshness', () => {
  it('prefers stats freshness when it is present', () => {
    expect(
      resolveEffectiveFreshness(
        {
          total_blocks: 1,
          total_transactions: 2,
          total_addresses: 3,
          latest_block: 4,
          average_block_time_ms: null,
          average_gas_price_gwei: null,
          network_utilization_percentage: null,
          transactions_today: null,
          freshness: {
            chain_head: { block_number: 10, timestamp: '2026-04-11T07:00:00Z', age_seconds: 1 },
            latest_indexed_block: { block_number: 10, timestamp: '2026-04-11T07:00:00Z', age_seconds: 1 },
            latest_indexed_transaction: { block_number: 9, timestamp: '2026-04-11T06:59:50Z', age_seconds: 11 },
            latest_non_empty_block: { block_number: 9, timestamp: '2026-04-11T06:59:50Z', age_seconds: 11, distance_from_head: 1 },
          },
          completeness: null,
          sampling: null,
        },
        {
          data: {
            freshness: {
              chain_head: { block_number: 20 },
            },
          },
        },
      ),
    ).toMatchObject({
      chain_head: { block_number: 10 },
      latest_non_empty_block: { distance_from_head: 1 },
    })
  })

  it('falls back to mission-control freshness when stats freshness is unavailable', () => {
    expect(
      resolveEffectiveFreshness(
        {
          total_blocks: 1,
          total_transactions: 2,
          total_addresses: 3,
          latest_block: 4,
          average_block_time_ms: null,
          average_gas_price_gwei: null,
          network_utilization_percentage: null,
          transactions_today: null,
          freshness: null,
          completeness: null,
          sampling: null,
        },
        {
          data: {
            freshness: {
              chain_head: { block_number: '20', timestamp: '2026-04-11T07:00:00Z', age_seconds: '2' },
              latest_indexed_block: { block_number: '20', timestamp: '2026-04-11T07:00:00Z', age_seconds: '2' },
              latest_indexed_transaction: { block_number: '19', timestamp: '2026-04-11T06:59:59Z', age_seconds: '3' },
              latest_non_empty_block: { block_number: '19', distance_from_head: '1' },
            },
          },
        },
      ),
    ).toMatchObject({
      chain_head: { block_number: 20, age_seconds: 2 },
      latest_indexed_transaction: { block_number: 19, age_seconds: 3 },
      latest_non_empty_block: { block_number: 19, distance_from_head: 1 },
    })
  })

  it('summarizes confidence in user-facing trust language', () => {
    expect(
      summarizeFreshnessConfidence(
        {
          total_blocks: 1,
          total_transactions: 2,
          total_addresses: 3,
          latest_block: 4,
          average_block_time_ms: null,
          average_gas_price_gwei: null,
          network_utilization_percentage: null,
          transactions_today: null,
          freshness: {
            chain_head: {
              block_number: 10,
              timestamp: '2026-04-11T07:00:00Z',
              age_seconds: 1,
              confidence: 'high',
              completeness: 'complete',
              source: 'reported',
            },
            latest_indexed_block: {
              block_number: 10,
              timestamp: '2026-04-11T07:00:00Z',
              age_seconds: 1,
            },
            latest_indexed_transaction: {
              block_number: 9,
              timestamp: '2026-04-11T06:59:50Z',
              age_seconds: 11,
              confidence: 'high',
              completeness: 'partial',
              source: 'reported',
            },
            latest_non_empty_block: {
              block_number: 9,
              timestamp: '2026-04-11T06:59:50Z',
              age_seconds: 11,
              distance_from_head: 1,
            },
          },
          completeness: null,
          sampling: null,
        },
        {
          data: {
            mode: {
              kind: 'snapshot',
            },
          },
        },
      ),
    ).toEqual([
      'Head: directly reported',
      'Transactions: partial visibility',
      'Feed: snapshot',
    ])
  })

  it('describes whether freshness comes from stats or mission-control fallback', () => {
    expect(
      resolveFreshnessSourceLabel(
        {
          total_blocks: 1,
          total_transactions: 2,
          total_addresses: 3,
          latest_block: 4,
          average_block_time_ms: null,
          average_gas_price_gwei: null,
          network_utilization_percentage: null,
          transactions_today: null,
          freshness: {
            chain_head: { block_number: 10 },
            latest_indexed_block: { block_number: 10 },
            latest_indexed_transaction: { block_number: 9 },
            latest_non_empty_block: { block_number: 9 },
          },
          completeness: null,
          sampling: null,
        },
        null,
      ),
    ).toBe('Based on public stats and indexed explorer freshness.')

    expect(
      resolveFreshnessSourceLabel(
        null,
        {
          data: {
            freshness: {
              chain_head: { block_number: 20 },
            },
          },
        },
      ),
    ).toBe('Based on mission-control freshness and latest visible public data.')
  })
})
