///|
/// A consumer built the way a third party would build one: it imports the
/// package and touches nothing but `pub` items. This example exercises the
/// public calculation and reconciliation APIs from a separate package.
///
/// Run it with `moon run examples/embed`.
fn main {
  // A single month: 30 000 gross, 3 000 social, 1 000 special deductions.
  let inputs : Array[@wagekit.MonthInput] = [
    { gross: 3_000_000L, social: 300_000L, special: 100_000L, other: 0L, },
  ]
  let schedule = @wagekit.cumulative_withholding(inputs)
  println("本月预扣 " + @wagekit.yuan_text(schedule[0].withheld) + " 元")
  println(
    "累计应纳税所得额 " +
    @wagekit.yuan_text(schedule[0].taxable_cum) +
    " 元",
  )

  // The same month, explained: which bracket, which rate, which arithmetic.
  let rules = @wagekit.default_rules()
  let explained = @wagekit.explain_withholding(rules, inputs)
  println(
    "档位 " +
    explained[0].bracket_no.to_string() +
    ":" +
    explained[0].formula,
  )

  // Hiring question: what gross pays 20 000 net?
  let offer = @wagekit.gross_up(2_000_000L, 300_000L, 0L, 0L) catch {
    TaxError(message) => {
      println("error: " + message)
      return
    }
  }
  println(
    "到手 " +
    @wagekit.yuan_text(offer.target_net) +
    " 元需要税前 " +
    @wagekit.yuan_text(offer.gross) +
    " 元" +
    (if offer.exact { "(精确)" } else { "(最小可行解)" }),
  )

  // Policy question: what would a 6 000 monthly deduction do to this payroll?
  let candidate = { ..rules, version: "preview", basic_deduction: 600_000L, }
  let impact = @wagekit.policy_impact(rules, candidate, inputs)
  println(
    "起征点 6000:全年少扣 " +
    @wagekit.yuan_text(-impact.total_delta) +
    " 元",
  )

  // Reconcile actual prior withholding, including an income drop and recovery.
  let history : Array[@wagekit.MonthInput] = [
    { gross: 1_500_000L, social: 0L, special: 0L, other: 0L, },
    { gross: 0L, social: 0L, special: 0L, other: 0L, },
    { gross: 1_500_000L, social: 0L, special: 0L, other: 0L, },
  ]
  let records : Array[@wagekit.PayrollLedger] = [
    {
      employee_id: "E001",
      inputs: history,
      actual_withheld: [30_000L, 0L, 30_000L],
    },
  ]
  let audit = @wagekit.reconcile(rules, records) catch {
    _ => abort("invalid payroll example")
  }
  if audit.differences != 1 || audit.rows[2].delta != 15_000L {
    abort("unexpected audit result")
  }
  println("第三期差额 " + @wagekit.yuan_text(audit.rows[2].delta) + " 元")

  // Assume complete annual data before comparing bonus treatments.
  let choice = @wagekit.bonus_compare_under(rules, 3_600_000L, [
    { gross: 2_000_000L, social: 0L, special: 0L, other: 0L, },
  ])
  println("完整年度示例奖金方案:" + choice.best)
}