import { describe, expect, it } from 'vitest'
import { EU_REDUCED_RATE_MATRIX, EU_VAT_SCHEDULES, IL_VAT_SCHEDULE } from './dataset.js'
import { normalizeToBasisPoints } from './resolve.js'

/**
 * Canonical IL VAT history (sourced from `IL_VAT_RATES`) as `[date, bp]`.
 * Pins every row date + normalized basis points so a silent edit to ANY middle
 * row (e.g. the 2005 .165→.16 reduction) fails — the money bug this module fears.
 */
const IL_CANONICAL: ReadonlyArray<readonly [string, bigint]> = [
  ['1976-07-01', 800n],
  ['1977-11-01', 1200n],
  ['1982-08-01', 1500n],
  ['1985-06-01', 1700n],
  ['1985-10-01', 1500n],
  ['1990-03-01', 1600n],
  ['1991-01-01', 1800n],
  ['1993-01-01', 1700n],
  ['2002-06-15', 1800n],
  ['2004-03-01', 1700n],
  ['2005-09-01', 1650n],
  ['2006-07-01', 1550n],
  ['2009-07-01', 1650n],
  ['2010-01-01', 1600n],
  ['2012-09-01', 1700n],
  ['2013-06-02', 1800n],
  ['2015-10-01', 1700n],
  ['2025-01-01', 1800n],
]

describe('IL_VAT_SCHEDULE dataset', () => {
  it('ships all 18 Israeli Tax Authority history rows, byte-faithful', () => {
    expect(IL_VAT_SCHEDULE).toHaveLength(IL_CANONICAL.length)
    IL_CANONICAL.forEach(([date, bp], i) => {
      const row = IL_VAT_SCHEDULE[i]
      expect(row?.effectiveFrom).toBe(date)
      expect(normalizeToBasisPoints(row!.value, row!.unit)).toBe(bp)
    })
  })

  it('is strictly date-ascending (resolve relies on order being well-defined)', () => {
    for (let i = 1; i < IL_VAT_SCHEDULE.length; i++) {
      expect(IL_VAT_SCHEDULE[i - 1]!.effectiveFrom < IL_VAT_SCHEDULE[i]!.effectiveFrom).toBe(true)
    }
  })
})

describe('EU dataset scaffold', () => {
  it('exposes per-country schedules separate from IL', () => {
    expect(Object.keys(EU_VAT_SCHEDULES).sort()).toEqual(['DE', 'FR'])
    expect(EU_VAT_SCHEDULES.DE?.[0]?.unit).toBe('percent')
  })

  it('documents reduced-category matrix structure in basis points (lossless for 5.5%)', () => {
    expect(EU_REDUCED_RATE_MATRIX.food?.DE).toBe(700)
    expect(EU_REDUCED_RATE_MATRIX.food?.FR).toBe(550)
  })
})
