/**
 * commerce blueprint · wiring seam for `@platform-modules/tax`.
 *
 * Adapter-minimalism (CLAUDE.md §4): tax is a SUBPATH-ONLY module (`/rates-table`). This seam resolves
 * the VAT rate for an order date and splits an INCLUSIVE (tax-included) gross price into net + vat.
 *
 * PATTERN-C (CLAUDE.md §3): pricing model is a host-level boundary driver. For INCLUSIVE pricing
 * (IL/EU), the rate is resolvable PRE-CHARGE, which is the whole reason tax is a shared primitive
 * sitting ABOVE billing rather than hidden inside it: the host computes net/vat here, BEFORE calling
 * billing — and billing imports ZERO tax (verified: packages/billing imports only db + ledger types).
 * The exclusive (US, at-charge) path is the one that "can hide in billing"; this blueprint shows the
 * inclusive path that motivates the standalone module.
 *
 * DATE CONTRACT: `resolveVatRate` takes a host-resolved `YYYY-MM-DD` legal-date STRING, never a JS
 * Date (a Date is an instant that only becomes a calendar day through a timezone the country-neutral
 * module cannot know). A real host passes `IL_VAT_SCHEDULE` / `EU_VAT_SCHEDULES` (also exported by the
 * module); this blueprint uses a fixed schedule so the composition assertions stay deterministic
 * regardless of dataset evolution.
 */
import {
  extractVat,
  resolveVatRate,
  type VatRate,
  type VatScheduleEntry,
} from '@platform-modules/tax/rates-table'

/** Fixed 18% schedule — stands in for the real `IL_VAT_SCHEDULE` so assertions are deterministic. */
const BLUEPRINT_SCHEDULE: readonly VatScheduleEntry[] = [
  { effectiveFrom: '2020-01-01', value: 18, unit: 'percent' },
]

export type OrderTax = { rate: VatRate; net: bigint; vat: bigint; gross: bigint }

/**
 * Split an inclusive gross price (agorot) into net + vat at the rate effective on `date`. The host
 * charges the gross via billing and uses net/vat for its invoice + VAT-liability accounting (host
 * domain — the blueprint computes the split, it does not own the chart of accounts).
 */
export function resolveInclusiveOrderTax(
  grossAgorot: bigint,
  date: string,
  schedule: readonly VatScheduleEntry[] = BLUEPRINT_SCHEDULE,
): OrderTax {
  const rate = resolveVatRate(schedule, date)
  const { net, vat } = extractVat(grossAgorot, rate)
  return { rate, net, vat, gross: grossAgorot }
}
