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

import { IL_VAT_RATES } from '../../../../../../packages/db/src/seed/vat-rates.seed'
import {
  applyInvoiceVat,
  decimalAmountToAgorot,
  extractInvoiceVat,
  calculateHostInvoiceVat,
} from '../tax'

const NET_SAMPLES = ['0.00', '0.01', '0.05', '0.50', '1.00', '10.99', '100.00', '1234.56']

describe('tax platform parity', () => {
  it('matches the host invoice VAT calculation across the IL VAT rate table', () => {
    for (const [, , vatRate] of IL_VAT_RATES) {
      for (const netAmount of NET_SAMPLES) {
        const host = calculateHostInvoiceVat(netAmount, vatRate)
        const platform = applyInvoiceVat(netAmount, vatRate)

        expect(platform).toEqual(host)

        const extracted = extractInvoiceVat(platform.gross, vatRate)
        expect(extracted.gross).toBe(platform.gross)
        expect(extracted.vat).toBe(platform.vat)
        expect(extracted.net).toBe(platform.net)
        expect(decimalAmountToAgorot(extracted.net) + decimalAmountToAgorot(extracted.vat))
          .toBe(decimalAmountToAgorot(extracted.gross))
      }
    }
  })

  it('preserves the 18% invoice exemplar exactly', () => {
    const host = calculateHostInvoiceVat('100.00', '0.1800')
    const platform = applyInvoiceVat('100.00', '0.1800')

    expect(host).toEqual({
      net: '100.00',
      vat: '18.00',
      gross: '118.00',
      vatRate: '0.1800',
    })
    expect(platform).toEqual(host)
  })
})
