import assert from "node:assert/strict";
import test from "node:test";
import { computeProcess, detectEmergency, detectStopPath } from "@/lib/mortgage/gates";
import { heuristicPatch, mergeProfile } from "@/lib/mortgage/extract";
import { emptyProfile } from "@/lib/mortgage/schema";
import { extractHealthPatch } from "@/lib/mortgage/health";
import { parseUsState } from "@/lib/mortgage/usGeo";

test("I rent stops", () => {
  assert.equal(detectStopPath("I rent"), "NO-MTG");
});

test("~240k 28 years both fills loan and coverage; step >= 2 after identity", () => {
  let p = mergeProfile(emptyProfile(), {
    name: "Alex",
    state: "TX",
    intent: "mortgage_protection",
    coverage_on_whom: "both",
    age: 40,
  });
  p = mergeProfile(p, heuristicPatch("~240k, 28 years, both on the loan"));
  assert.equal(p.loan_balance, 240000);
  assert.equal(p.loan_term_years, 28);
  assert.equal(p.coverage_on_whom, "both");
  const proc = computeProcess(p);
  assert.ok(proc.step >= 2);
});

test("step 2 does not require cancer", () => {
  const p = mergeProfile(emptyProfile(), {
    name: "Ann",
    state: "FL",
    intent: "mortgage_protection",
    coverage_on_whom: "primary",
    age: 35,
    loan_balance: 200000,
  });
  const proc = computeProcess(p);
  assert.equal(proc.step, 3);
  assert.ok(!proc.missingRequired.includes("diseases"));
});

test("emergency language", () => {
  assert.equal(detectEmergency("I have chest pain right now"), true);
});

test("BMI merge", () => {
  const p = mergeProfile(emptyProfile(), { height_ft_in: "5'10\"", weight_lb: 180 });
  assert.equal(p.bmi_computed, 25.8);
});

test("name from i am sunil verma; live/leave does not overwrite", () => {
  let p = mergeProfile(emptyProfile(), heuristicPatch("Hi i am sunil verma"));
  assert.equal(p.name?.toLowerCase(), "sunil verma");
  p = mergeProfile(p, heuristicPatch("I am leave in Hanumangarh at Rajsthan"));
  assert.equal(p.name?.toLowerCase(), "sunil verma");
  assert.equal(p.state, undefined);
});

test("health screens persist diabetes and rest-good without looping", () => {
  let p = emptyProfile();
  p = mergeProfile(p, extractHealthPatch("I have a diabetes", "Any history of diabetes?", p));
  p = mergeProfile(p, extractHealthPatch("2019", "when were you diagnosed", p));
  p = mergeProfile(p, extractHealthPatch("Metformin 500 mg twice daily", "medication", p));
  p = mergeProfile(p, extractHealthPatch("no", "any complications", p));
  p = mergeProfile(
    p,
    extractHealthPatch(
      "Kindly note i am only cigar smoker with some diabtic condition rest my health is very good",
      undefined,
      p
    )
  );
  assert.equal(p.disease_screens?.diabetes, "yes");
  assert.equal(p.disease_screens?.cancer, "no");
  assert.equal(p.disease_screens?.heart, "no");
  assert.equal(p.diseases_complete, true);
  assert.ok((p.impairments || []).some((i) => i.module === "diabetes"));
});

test("my name is John Michael Smith is not overwritten by cigar sentence", () => {
  let p = mergeProfile(emptyProfile(), heuristicPatch("Yes My name is John Michael Smith"));
  assert.equal(p.name, "John Michael Smith");
  p = mergeProfile(
    p,
    heuristicPatch("Kindly note i am only cigar smoker with some diabtic condition rest my health is very good")
  );
  assert.equal(p.name, "John Michael Smith");
});

test("Hi is not Hawaii and does not extract a name", () => {
  assert.equal(parseUsState("Hi"), null);
  assert.equal(parseUsState("hello"), null);
  assert.equal(parseUsState("Hawaii"), "HI");
  assert.equal(parseUsState("Texas"), "TX");
  const p = mergeProfile(emptyProfile(), heuristicPatch("Hi"));
  assert.equal(p.name, undefined);
  assert.equal(p.state, undefined);
});
