import { expect, test } from "@playwright/test";
import {
  createCase,
  openCaseOnStep,
  profileRow,
  sendChat,
  sendMany,
  STEP_SCRIPT,
  TO_STEP_4,
} from "./helpers";

test("S5-01 after clean health the assistant asks annual income (does not skip step 5)", async ({
  request,
  page,
}) => {
  const created = await createCase(request);
  const c = await sendMany(request, created.caseId, [...TO_STEP_4, "none of those"]);
  expect(c.process.step).toBe(5);
  expect(c.process.missingRequired).toEqual(expect.arrayContaining(["annual_income"]));
  expect(c.messages[c.messages.length - 1].content).toMatch(/income/i);
  await openCaseOnStep(page, created.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Spouse / income");
  await expect(profileRow(page, "annual_income")).toContainText("We’ll ask this next.");
});

test("S5-02 my annual income is 85k", async ({ request, page }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, [...TO_STEP_4, STEP_SCRIPT.diseases]);
  const c = await sendChat(request, created.caseId, "My annual income is 85000");
  expect(c.profile.annual_income).toBe(85000);
  expect(c.profile.loan_balance).toBe(180000);
  expect(c.process.step).toBeGreaterThanOrEqual(6);
  await openCaseOnStep(page, created.caseId);
  await expect(profileRow(page, "annual_income")).toContainText("85000");
  await expect(profileRow(page, "loan_balance")).toContainText("180000");
});

test("S5-03 85k natural language", async ({ request }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, [...TO_STEP_4, STEP_SCRIPT.diseases]);
  const c = await sendChat(request, created.caseId, "my annual income is 85k");
  expect(c.profile.annual_income).toBe(85000);
  expect(c.profile.loan_balance).toBe(180000);
});

test("S5-04 dollar 85,000", async ({ request }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, [...TO_STEP_4, STEP_SCRIPT.diseases]);
  const c = await sendChat(request, created.caseId, "I make $85,000 a year");
  expect(c.profile.annual_income).toBe(85000);
});

test("S5-05 missing income stays on Spouse / income", async ({ request, page }) => {
  const created = await createCase(request);
  const c = await sendMany(request, created.caseId, [...TO_STEP_4, STEP_SCRIPT.diseases]);
  expect(c.process.step).toBe(5);
  await openCaseOnStep(page, created.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Spouse / income");
});

test("S5-06 co-borrower requires spouse income then applicant income", async ({ request, page }) => {
  const created = await createCase(request);
  const c = await sendMany(request, created.caseId, [
    "My name is Alex Rivera",
    "Ohio",
    "mortgage protection for both of us on the loan",
    STEP_SCRIPT.age,
    "loan balance 250k usd, 25 years left",
    STEP_SCRIPT.build,
    STEP_SCRIPT.diseases,
  ]);
  expect(c.profile.co_borrower_flag).toBe(true);
  expect(c.process.step).toBe(5);
  expect(c.process.missingRequired).toEqual(
    expect.arrayContaining(["spouse_age_sex", "spouse_annual_income", "annual_income"])
  );

  await sendChat(request, created.caseId, "spouse is 36");
  const sp = await sendChat(request, created.caseId, "spouse income is 40k");
  expect(sp.profile.spouse_annual_income).toBe(40000);
  expect(sp.profile.loan_balance).toBe(250000);
  const mine = await sendChat(request, created.caseId, "my annual income is 85k");
  expect(mine.profile.annual_income).toBe(85000);
  await sendChat(request, created.caseId, "beneficiary is my spouse");

  await openCaseOnStep(page, created.caseId);
  await expect(profileRow(page, "spouse_annual_income")).toContainText("40000");
  await expect(profileRow(page, "annual_income")).toContainText("85000");
});
