import assert from "node:assert/strict";
import test from "node:test";
import {
  extractHealthPatch,
  healthGateComplete,
  nextDiseaseQuestion,
  remainingDiseaseModules,
  R_DISEASE,
} from "@/lib/mortgage/health";
import { emptyProfile, type ApplicantProfile } from "@/lib/mortgage/schema";
import { mergeProfile } from "@/lib/mortgage/extract";

test("R_DISEASE has five screens", () => {
  assert.deepEqual([...R_DISEASE], ["heart", "diabetes", "cancer", "kidney", "disabled"]);
});

test("none of those completes health gate", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("none of those"));
  assert.equal(p.diseases_none_reported, true);
  assert.equal(healthGateComplete(p), true);
  assert.equal(remainingDiseaseModules(p).length, 0);
});

test("no medical issues completes; I'm healthy alone does not", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("I am healthy no medical issues"));
  assert.equal(healthGateComplete(p), true);
  const vague = mergeProfile(emptyProfile(), extractHealthPatch("I'm healthy"));
  assert.equal(healthGateComplete(vague), false);
});

test("no on asked cancer does not clear diabetes yes", () => {
  let p = mergeProfile(emptyProfile(), extractHealthPatch("I have diabetes", "diabetes?"));
  p = mergeProfile(p, extractHealthPatch("no", "any cancer history?"));
  assert.equal(p.disease_screens?.diabetes, "yes");
  assert.equal(p.disease_screens?.cancer, "no");
});

test("year only attaches to diabetes", () => {
  const base = mergeProfile(emptyProfile(), extractHealthPatch("diabetes"));
  const p = mergeProfile(base, extractHealthPatch("2019", "when diagnosed diabetes"));
  const dm = (p.impairments || []).find((i) => i.module === "diabetes");
  assert.equal(dm?.attributes.diagnosed_year, "2019");
});

test("type 1 vs type 2", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("Type 1 diabetes"));
  assert.equal((p.impairments || []).find((i) => i.module === "diabetes")?.attributes.type, "Type 1");
});

test("insulin and metformin", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("I take insulin"));
  assert.equal(p.disease_screens?.diabetes, "yes");
});

test("complications no", () => {
  let p = mergeProfile(emptyProfile(), extractHealthPatch("diabetes"));
  p = mergeProfile(p, extractHealthPatch("no", "any complications from diabetes", p));
  const dm = (p.impairments || []).find((i) => i.module === "diabetes");
  assert.equal(dm?.attributes.complications, false);
});

test("lung copd", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("I have COPD"));
  assert.equal(p.disease_screens?.lung, "yes");
});

test("disabled wheelchair", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("I use a wheelchair"));
  assert.equal(p.disease_screens?.disabled, "yes");
});

test("lastAsked immune maps to disabled no", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("no", "epilepsy or lupus?"));
  assert.equal(p.disease_screens?.disabled, "no");
});

test("lastAsked joint maps to disabled", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("no", "arthritis or muscle disease?"));
  assert.equal(p.disease_screens?.disabled, "no");
});

test("lastAsked lung", () => {
  const p = mergeProfile(emptyProfile(), extractHealthPatch("no", "any lung or COPD history?"));
  assert.equal(p.disease_screens?.lung, "no");
});

test("lastAsked heart and kidney", () => {
  let p = mergeProfile(emptyProfile(), extractHealthPatch("no", "heart attack or stent?"));
  assert.equal(p.disease_screens?.heart, "no");
  p = mergeProfile(p, extractHealthPatch("no", "kidney failure or dialysis?"));
  assert.equal(p.disease_screens?.kidney, "no");
});

test("nothing else marks remaining no", () => {
  let p = mergeProfile(emptyProfile(), extractHealthPatch("diabetes type 2"));
  p = mergeProfile(p, extractHealthPatch("nothing else"));
  assert.equal(p.diseases_complete, true);
});

test("nextDiseaseQuestion", () => {
  assert.match(nextDiseaseQuestion(emptyProfile()), /heart|diabetes|cancer/i);
  const done: ApplicantProfile = mergeProfile(emptyProfile(), {
    diseases_complete: true,
    disease_screens: { heart: "no", diabetes: "no", cancer: "no", kidney: "no", disabled: "no" },
  });
  assert.match(nextDiseaseQuestion(done), /health screens/i);
  const dm = mergeProfile(emptyProfile(), {
    disease_screens: { diabetes: "yes" },
    impairments: [{ module: "diabetes", attributes: { disclosed: true } }],
  });
  assert.match(nextDiseaseQuestion(dm), /Type 1 or Type 2|diabetes/i);
  const dm2 = mergeProfile(emptyProfile(), {
    disease_screens: { diabetes: "yes" },
    impairments: [{ module: "diabetes", attributes: { type: "Type 2" } }],
  });
  assert.match(nextDiseaseQuestion(dm2), /year were you diagnosed/i);
  const dm3 = mergeProfile(emptyProfile(), {
    disease_screens: { diabetes: "yes", heart: "no", cancer: "no", kidney: "no", disabled: "no" },
    impairments: [{ module: "diabetes", attributes: { type: "Type 2", diagnosed_year: "2019" } }],
  });
  assert.match(nextDiseaseQuestion(dm3), /health screens|disability/i);
});

test("cancer heart kidney metformin type 2 year", () => {
  assert.equal(mergeProfile(emptyProfile(), extractHealthPatch("I had cancer")).disease_screens?.cancer, "yes");
  assert.equal(mergeProfile(emptyProfile(), extractHealthPatch("heart attack last year")).disease_screens?.heart, "yes");
  assert.equal(mergeProfile(emptyProfile(), extractHealthPatch("on dialysis")).disease_screens?.kidney, "yes");
  const met = mergeProfile(emptyProfile(), extractHealthPatch("Metformin 500 mg"));
  assert.equal(met.disease_screens?.diabetes, "yes");
  const t2 = mergeProfile(emptyProfile(), extractHealthPatch("Type 2", "diabetes type?"));
  assert.equal((t2.impairments || []).find((i) => i.module === "diabetes")?.attributes.type, "Type 2");
});
