import assert from "node:assert/strict";
import test from "node:test";
import { replayConversation, replayProcess } from "@/lib/mortgage/replay";
import type { ChatTurn } from "@/lib/mortgage/replay";

/** Same IDs as the live MySQL cases under review. */
export const CASE_C5 = "c5c60f11-879e-4121-bcfa-26bad83e5603";
export const CASE_28D = "28d44cd5-42b0-4f0b-acd1-6a10457f73dd";

function users(lines: string[]): ChatTurn[] {
  return lines.map((content) => ({ role: "user" as const, content }));
}

test(`${CASE_C5} fills mortgage, demographics, BMI, tobacco sections`, () => {
  const { profile, process } = replayProcess(
    users([
      "Hello",
      "My Self  John Smith is a fictional 52-year-old",
      "TX",
      "13802070381",
      "john.smith@gmail.com",
      "and my selft only",
      "05/15/1985",
      "250K USD",
      "I owe 250K and it will complete next 9 year 5 months",
      "My height 5'11",
      "118 Kg",
      "I used e-cigar only",
      "I am not using any tobacco and nicotine so keep in mind",
    ])
  );
  assert.equal(profile.name, "John Smith");
  assert.equal(profile.state, "TX");
  assert.equal(profile.phone, "13802070381");
  assert.equal(profile.email, "john.smith@gmail.com");
  assert.equal(profile.coverage_on_whom, "primary");
  assert.equal(profile.loan_balance, 250000);
  assert.equal(profile.loan_term_years, 9);
  assert.ok(profile.height_ft_in?.startsWith("5'11"));
  assert.equal(profile.weight_lb, 260);
  assert.ok(profile.bmi_computed && profile.bmi_computed > 30);
  assert.equal(profile.tobacco_answered, true);
  assert.deepEqual(profile.tobacco_products, ["None"]);
  assert.equal(profile.beneficiary_relationship, "self");
  assert.ok(process.step >= 3, `expected step>=3 got ${process.step}`);
});

test(`${CASE_28D} fills all profile sections through health and household`, () => {
  const { profile, process } = replayProcess(
    users([
      "Yes My name is John Michael Smith",
      "13802070380",
      "john.smith@gmail.com",
      "250K USD",
      "Texas",
      "i need mortgage protection",
      "only me",
      "05/15/1985",
      "my height 5 feet 11 inches and weight is 118kg",
      "I am regular smoker of e-cigar",
      "I have a diabetes",
      "2019",
      "Metformin 500 mg twice daily",
      "Kindly note i am only cigar smoker with some diabtic condition rest my health is very good",
      "Type 2",
    ])
  );
  assert.equal(profile.name, "John Michael Smith");
  assert.equal(profile.state, "TX");
  assert.equal(profile.loan_balance, 250000);
  assert.equal(profile.coverage_on_whom, "primary");
  assert.ok(profile.height_ft_in);
  assert.ok(profile.weight_lb);
  assert.ok(profile.bmi_computed);
  assert.equal(profile.tobacco_answered, true);
  assert.equal(profile.disease_screens?.diabetes, "yes");
  assert.equal(profile.disease_screens?.cancer, "no");
  assert.equal(profile.disease_screens?.heart, "no");
  assert.equal(profile.diseases_complete, true);
  assert.equal(profile.beneficiary_relationship, "self");
  assert.ok(profile.decision_result);
  assert.ok((profile.carrier_shortlist || []).length > 0);
  assert.ok(process.step >= 7, `expected step>=7 got ${process.step} missing ${process.missingRequired}`);
});

test("full applicant profile walk fills every pane section 0-8", () => {
  const p = replayConversation([
    { role: "user", content: "My name is Pat Lee" },
    { role: "user", content: "Florida" },
    { role: "user", content: "I want mortgage protection on me only" },
    { role: "user", content: "I am 40 years old" },
    { role: "user", content: "loan balance 180k usd, 20 years left" },
    { role: "user", content: "5'8\" 160 lb non-smoker" },
    { role: "user", content: "no diseases none of those" },
    { role: "user", content: "beneficiary is my spouse" },
  ]);
  assert.equal(p.name, "Pat Lee");
  assert.equal(p.state, "FL");
  assert.equal(p.intent, "mortgage_protection");
  assert.equal(p.coverage_on_whom, "primary");
  assert.equal(p.age, 40);
  assert.equal(p.loan_balance, 180000);
  assert.equal(p.loan_term_years, 20);
  assert.ok(p.bmi_computed);
  assert.equal(p.tobacco_answered, true);
  assert.equal(p.diseases_complete || p.diseases_none_reported, true);
  assert.ok(p.beneficiary_relationship);
  assert.ok(p.decision_result);
  assert.ok(p.carrier_shortlist?.length);
});
