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

test("S4-01 none of those completes R screens and asks income", async ({ request, page }) => {
  const created = await createCase(request);
  const atFour = await sendMany(request, created.caseId, TO_STEP_4);
  expect(atFour.process.step).toBe(4);
  const lastAsk = atFour.messages[atFour.messages.length - 1].content;
  expect(lastAsk).toMatch(/heart|diabetes|cancer|kidney|disab|none/i);

  const c = await sendChat(request, created.caseId, "I don't have any of those conditions");
  expect(c.profile.diseases_none_reported).toBe(true);
  expect(c.profile.diseases_complete).toBe(true);
  expect(c.profile.disease_screens).toMatchObject({
    heart: "no",
    diabetes: "no",
    cancer: "no",
    kidney: "no",
    disabled: "no",
  });
  expect(c.process.step).toBe(5);
  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, "heart")).toContainText("no");
  await expect(profileRow(page, "diabetes")).toContainText("no");
});

test("S4-03 type 2 diabetes 2015 then rest of health good", async ({ request, page }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, TO_STEP_4);
  const dm = await sendChat(request, created.caseId, "I have type 2 diabetes, diagnosed in 2015");
  expect(dm.profile.disease_screens?.diabetes).toBe("yes");
  const imp = (dm.profile.impairments as { module: string; attributes: Record<string, unknown> }[]).find(
    (i) => i.module === "diabetes"
  );
  expect(imp?.attributes.type).toBe("Type 2");
  expect(imp?.attributes.diagnosed_year).toBe("2015");

  const rest = await sendChat(request, created.caseId, "Rest of my health is good");
  expect(rest.profile.disease_screens?.diabetes).toBe("yes");
  expect(rest.profile.disease_screens?.heart).toBe("no");
  expect(rest.profile.diseases_complete).toBe(true);
  expect(rest.process.step).toBe(5);

  await openCaseOnStep(page, created.caseId);
  await expect(profileRow(page, "diabetes")).toContainText("yes");
  await expect(profileRow(page, "impairments")).toContainText("diabetes");
  await expect(profileRow(page, "impairments")).not.toContainText("[object Object]");
});

test("S4-04 metformin diabetes", async ({ request }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, TO_STEP_4);
  const c = await sendChat(request, created.caseId, "I'm on metformin for diabetes");
  expect(c.profile.disease_screens?.diabetes).toBe("yes");
  const imp = (c.profile.impairments as { module: string; attributes: Record<string, unknown> }[]).find(
    (i) => i.module === "diabetes"
  );
  expect(String(imp?.attributes.medication)).toMatch(/metformin/i);
});

test("S4-08 heart attack", async ({ request, page }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, TO_STEP_4);
  const c = await sendChat(request, created.caseId, "I had a heart attack in 2019");
  expect(c.profile.disease_screens?.heart).toBe("yes");
  await openCaseOnStep(page, created.caseId);
  await expect(profileRow(page, "heart")).toContainText("yes");
});

test("S4-09 dialysis kidney", async ({ request }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, TO_STEP_4);
  const c = await sendChat(request, created.caseId, "I'm on dialysis");
  expect(c.profile.disease_screens?.kidney).toBe("yes");
});

test("S4-10 cancer chemo", async ({ request }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, TO_STEP_4);
  const c = await sendChat(request, created.caseId, "History of cancer, had chemo");
  expect(c.profile.disease_screens?.cancer).toBe("yes");
});

test("S4-12 COPD lung", async ({ request, page }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, TO_STEP_4);
  const c = await sendChat(request, created.caseId, "I have COPD, use oxygen");
  expect(c.profile.disease_screens?.lung).toBe("yes");
  await openCaseOnStep(page, created.caseId);
  await expect(profileRow(page, "lung")).toContainText("yes");
});

test("S4-17 diabetes and heart in one message", async ({ request }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, TO_STEP_4);
  const c = await sendChat(request, created.caseId, "I have diabetes and had a heart attack");
  expect(c.profile.disease_screens?.diabetes).toBe("yes");
  expect(c.profile.disease_screens?.heart).toBe("yes");
});

test("S4-13 does not re-ask completed heart after no", async ({ request }) => {
  const created = await createCase(request);
  const atFour = await sendMany(request, created.caseId, TO_STEP_4);
  const asked = atFour.messages[atFour.messages.length - 1].content;
  const denied = await sendChat(request, created.caseId, "no");
  expect(denied.process.step).toBe(4);
  const nextQ = denied.messages[denied.messages.length - 1].content;
  expect(nextQ).not.toBe(asked);
});
