import assert from "node:assert/strict";
import test from "node:test";
import { computeProcess, missingRequired, STEP_META } from "@/lib/mortgage/gates";
import { remainingDiseaseModules, R_DISEASE } from "@/lib/mortgage/health";
import { emptyProfile } from "@/lib/mortgage/schema";
import { mergeProfile } from "@/lib/mortgage/extract";

/**
 * Dataset html/vertical-datasets/dataset-mortgage-protection.html vs current gates.ts.
 * These tests record CURRENT app gates. Mismatches are BUSINESS CLARIFICATION, not failures.
 */

test("UI process is steps 0–8 only (process HTML 9–11 are human, not in STEP_META)", () => {
  assert.equal(Object.keys(STEP_META).length, 9);
  for (let i = 0; i <= 8; i++) assert.ok(STEP_META[i as keyof typeof STEP_META]);
});

test("gate2 matches process HTML: age or DOB + loan_balance (not sex, not loan_term_years)", () => {
  const p = mergeProfile(emptyProfile(), {
    name: "Ann",
    state: "TX",
    intent: "mortgage_protection",
    coverage_on_whom: "primary",
    age: 42,
    loan_balance: 200000,
  });
  assert.equal(computeProcess(p).step, 3);
  assert.equal(p.sex, undefined);
  assert.equal(p.loan_term_years, undefined);
});

test("dataset marks sex and loan_term_years R but current missingRequired(step 2) does not require them", () => {
  const p = mergeProfile(emptyProfile(), {
    name: "Ann",
    state: "TX",
    intent: "mortgage_protection",
    coverage_on_whom: "primary",
  });
  const miss = missingRequired(p, 2);
  assert.ok(miss.includes("date_of_birth"));
  assert.ok(miss.includes("loan_balance"));
  assert.ok(!miss.includes("sex"));
  assert.ok(!miss.includes("loan_term_years"));
});

test("dataset marks annual_income S; current gate5 still requires annual_income", () => {
  const p = mergeProfile(emptyProfile(), {
    name: "Ann",
    state: "TX",
    intent: "mortgage_protection",
    coverage_on_whom: "primary",
    age: 40,
    loan_balance: 200000,
    height_ft_in: "5'6\"",
    weight_lb: 140,
    tobacco_answered: true,
    diseases_complete: true,
    beneficiary_relationship: "self",
  });
  assert.ok(missingRequired(p, 5).includes("annual_income"));
  assert.equal(computeProcess(p).step, 5);
});

test("dataset tobacco months_since_last_use is R; current gate3 does not require it", () => {
  const p = mergeProfile(emptyProfile(), {
    name: "Ann",
    state: "TX",
    intent: "mortgage_protection",
    coverage_on_whom: "primary",
    age: 40,
    loan_balance: 200000,
    height_ft_in: "5'6\"",
    weight_lb: 140,
    tobacco_answered: true,
    tobacco_products: ["None"],
  });
  assert.ok(!missingRequired(p, 3).includes("months_since_last_use"));
  assert.equal(computeProcess(p).step, 4);
});

test("process HTML lists lung in disease interview; current remainingDiseaseModules is R_DISEASE without lung", () => {
  assert.deepEqual([...R_DISEASE], ["heart", "diabetes", "cancer", "kidney", "disabled"]);
  assert.ok(!remainingDiseaseModules(emptyProfile()).includes("lung"));
});
