// 면세/과세 금액 계산 (단가는 VAT 포함가 가정)

export interface PricingLine {
  unitPrice: number;
  qty: number;
  isTaxFree: boolean;
}

export interface PricingResult {
  supplyAmount: number;
  vatAmount: number;
  totalAmount: number;
}

export function calcLine(line: PricingLine): PricingResult {
  const qty = Number(line.qty) || 0;
  const price = Number(line.unitPrice) || 0;
  const totalAmount = Math.round(price * qty);
  if (line.isTaxFree) {
    return { supplyAmount: totalAmount, vatAmount: 0, totalAmount };
  }
  const supply = Math.round(totalAmount / 1.1);
  return {
    supplyAmount: supply,
    vatAmount: totalAmount - supply,
    totalAmount,
  };
}

export interface OrderTotals {
  supply: number;
  vat: number;
  total: number;
  taxFree: number;
  taxable: number;
}

export function sumTotals(lines: { supplyAmount: number; vatAmount: number; totalAmount: number; isTaxFree: boolean }[]): OrderTotals {
  let supply = 0, vat = 0, total = 0, taxFree = 0, taxable = 0;
  for (const l of lines) {
    supply += l.supplyAmount;
    vat += l.vatAmount;
    total += l.totalAmount;
    if (l.isTaxFree) taxFree += l.supplyAmount;
    else taxable += l.supplyAmount;
  }
  return { supply, vat, total, taxFree, taxable };
}
