/**
 * Deploy gate — must pass before production build/restart.
 * Catches regressions that previously shipped to mortgage.gventure.live:
 * - health-insurance knowledge stopping the chat (WR-HEALTH hard stop)
 * - historical stroke falsely triggering EMERGENCY
 * - joke "no mortgage on Earth" falsely triggering NO-MTG
 * - progress panel leaking raw gate keys (name_or_phone)
 * - multi-field lead message dropping name
 */
import assert from "node:assert/strict";
import test from "node:test";
import {
  detectEmergency,
  detectStopPath,
  formatMissingRequired,
  isHardStop,
  normalizeStopPath,
} from "@/lib/mortgage/gates";
import { heuristicPatch, mergeProfile } from "@/lib/mortgage/extract";
import { emptyProfile } from "@/lib/mortgage/schema";
import { extractPersonName } from "@/lib/mortgage/usGeo";

test("GATE-STOP-01 health insurance is soft-clarify only (never hard-stop)", () => {
  assert.equal(detectStopPath("is this health insurance?"), "WR-HEALTH");
  assert.equal(detectStopPath("I need health insurance"), "WR-HEALTH");
  assert.equal(isHardStop("WR-HEALTH"), false);
  assert.equal(isHardStop("NO-MTG"), true);
  assert.equal(isHardStop("WR-LOAN"), true);
  assert.equal(isHardStop("DNC"), true);
  assert.equal(isHardStop("EMERGENCY"), true);
});

test("GATE-STOP-01b free-form LLM health stop must not hard-stop", () => {
  assert.equal(normalizeStopPath("Wrong product — wants health insurance"), "WR-HEALTH");
  assert.equal(isHardStop(normalizeStopPath("Wrong product — wants health insurance")), false);
  assert.equal(isHardStop("Wrong product — wants health insurance"), false);
  assert.equal(normalizeStopPath("I want health insurance intent"), "WR-HEALTH");
  assert.equal(normalizeStopPath("something weird the model invented"), null);
  assert.equal(isHardStop("something weird the model invented"), false);
});

test("GATE-STOP-01c I want health insurance is soft like knowledge Q", () => {
  assert.equal(detectStopPath("I want health insurance."), "WR-HEALTH");
  assert.equal(isHardStop(detectStopPath("I want health insurance.")), false);
});

test("GATE-STOP-02 genuine disqualifiers still hard-stop", () => {
  assert.equal(detectStopPath("I rent"), "NO-MTG");
  assert.equal(detectStopPath("My mortgage is already paid off."), "NO-MTG");
  assert.equal(detectStopPath("I want to refinance my rate"), "WR-LOAN");
  assert.equal(detectStopPath("do not call"), "DNC");
});

test("GATE-STOP-03 joke / absurd no-mortgage must not stop", () => {
  assert.equal(detectStopPath("no mortgage on Earth"), null);
  assert.equal(detectStopPath("It sounds like you might not have a mortgage here on Earth"), null);
});

test("GATE-EMERG-01 historical or negated stroke is not emergency", () => {
  assert.equal(detectEmergency("none of those, no stroke history"), false);
  assert.equal(detectEmergency("I had a stroke 10 years ago"), false);
  assert.equal(detectEmergency("chest pain now"), true);
});

test("GATE-UI-01 missing fields use human labels not raw keys", () => {
  assert.equal(
    formatMissingRequired(["name_or_phone", "state"]),
    "Name or phone · U.S. state"
  );
  assert.ok(!formatMissingRequired(["name_or_phone"]).includes("name_or_phone"));
});

test("GATE-LOAN-NEG signed negative loan not stored", () => {
  const p = mergeProfile(emptyProfile(), heuristicPatch("loan balance is -100000"));
  assert.equal(p.loan_balance, undefined);
});

test("GATE-DOB-FUTURE future DOB not stored", () => {
  const p = mergeProfile(emptyProfile(), heuristicPatch("My DOB is 01/01/2035"));
  assert.equal(p.date_of_birth, undefined);
});

test("GATE-EXTRACT-01 multi-field lead captures name + state + phone", () => {
  const p = mergeProfile(
    emptyProfile(),
    heuristicPatch("John Smith from Texas, 555-123-4567")
  );
  assert.equal(p.name, "John Smith");
  assert.equal(p.state, "TX");
  assert.equal(p.phone, "555-123-4567");
  assert.equal(extractPersonName("John Smith from Texas, 555-123-4567"), "John Smith");
});
