import assert from "node:assert/strict";
import test from "node:test";
import { heuristicPatch, mergeProfile } from "@/lib/mortgage/extract";
import { emptyProfile } from "@/lib/mortgage/schema";

test("merge ignores junk name", () => {
  const p = mergeProfile(emptyProfile(), { name: "only cigar smoker" });
  assert.equal(p.name, undefined);
});

test("merge keeps longer plausible name", () => {
  let p = mergeProfile(emptyProfile(), { name: "John Michael Smith" });
  p = mergeProfile(p, { name: "John" });
  assert.equal(p.name, "John Michael Smith");
});

test("merge state must be US", () => {
  const p = mergeProfile(emptyProfile(), { state: "ZZ" });
  assert.equal(p.state, undefined);
  assert.equal(mergeProfile(emptyProfile(), { state: "Texas" }).state, "TX");
});

test("merge impairments by module", () => {
  let p = mergeProfile(emptyProfile(), {
    impairments: [{ module: "diabetes", attributes: { type: "Type 2" }, path_answers: ["a"] }],
  });
  p = mergeProfile(p, {
    impairments: [{ module: "diabetes", attributes: { diagnosed_year: "2019" }, path_answers: ["b"] }],
  });
  assert.equal(p.impairments?.[0].attributes.type, "Type 2");
  assert.equal(p.impairments?.[0].attributes.diagnosed_year, "2019");
});

test("heuristic loan kg email phone height", () => {
  const p = mergeProfile(
    emptyProfile(),
    heuristicPatch("250K USD, 9 years left, 5 feet 11 inches and 118kg, john@x.com, 13802070380")
  );
  assert.equal(p.loan_balance, 250000);
  assert.equal(p.loan_term_years, 9);
  assert.equal(p.weight_lb, 260);
  assert.ok(p.height_ft_in);
  assert.equal(p.email, "john@x.com");
  assert.equal(p.phone, "13802070380");
});

test("heuristic coverage both and primary", () => {
  assert.equal(heuristicPatch("cover both of us on the loan").coverage_on_whom, "both");
  assert.equal(heuristicPatch("only me").coverage_on_whom, "primary");
});

test("heuristic intent and dob age", () => {
  assert.equal(heuristicPatch("mortgage protection please").intent, "mortgage_protection");
  assert.equal(heuristicPatch("05/15/1985").date_of_birth, "05/15/1985");
  assert.equal(heuristicPatch("I am 40 years old").age, 40);
});

test("heuristic tobacco branches", () => {
  assert.deepEqual(heuristicPatch("I am a non-smoker").tobacco_products, ["None"]);
  assert.deepEqual(heuristicPatch("I used e-cigar").tobacco_products, ["E-cigar"]);
  assert.deepEqual(heuristicPatch("I am a smoker").tobacco_products, ["Smoker"]);
  const none = heuristicPatch("No never", { lastAssistant: "Any tobacco or nicotine?" });
  assert.deepEqual(none.tobacco_products, ["None"]);
});

test("heuristic spouse and beneficiary", () => {
  assert.ok(heuristicPatch("spouse is 42").spouse_age_sex);
  assert.equal(heuristicPatch("beneficiary is my spouse").beneficiary_relationship, "spouse");
  assert.equal(heuristicPatch("leave it to my kids").beneficiary_relationship, "family");
});

test("heuristic dollar loan", () => {
  const p = heuristicPatch("I owe $180,000 on the mortgage");
  assert.equal(p.loan_balance, 180000);
});

test("heuristic none of those diseases without diabetes", () => {
  const p = heuristicPatch("none of those");
  assert.equal(p.diseases_none_reported, true);
});

test("mergeProfile ignores invalid patch objects", () => {
  const p = mergeProfile(emptyProfile(), { age: "not-a-number" });
  assert.equal(p.age, undefined);
});

test("heuristic pounds and primary-only aliases", () => {
  const wt = heuristicPatch("I weigh 180 pounds");
  assert.equal(wt.weight_lb, 180);
  const just = heuristicPatch("just me");
  assert.equal(just.coverage_on_whom, "primary");
  assert.equal(just.beneficiary_relationship, "self");
});

test("heuristic k thousand loan when mortgage mentioned", () => {
  const p = heuristicPatch("loan balance is 180 thousand");
  assert.equal(p.loan_balance, 180000);
});

test("heuristic income vs loan and spouse income", () => {
  const mine = heuristicPatch("my annual income is 85k");
  assert.equal(mine.annual_income, 85000);
  assert.equal(mine.loan_balance, undefined);
  const spouse = heuristicPatch("spouse income is 40k");
  assert.equal(spouse.spouse_annual_income, 40000);
  const loan = heuristicPatch("loan balance 180k usd");
  assert.equal(loan.loan_balance, 180000);
  const fromAsk = heuristicPatch("85000", { lastAssistant: "About what is your annual household income" });
  assert.equal(fromAsk.annual_income, 85000);
});
