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

test("SP-01 I rent stops chat and progress", async ({ page }) => {
  await waitForChatReady(page);
  await sendUi(page, "I rent", { allowDisabled: true });
  await expect(page.getByTestId("process-current")).toHaveText("Stopped");
  await expect(page.getByLabel("Chat message")).toBeDisabled();
  await expect(page.getByText(/mortgage protection is for people with a home loan/i)).toBeVisible();
});

test("SP-03 refinance without protect stops WR-LOAN", async ({ request, page }) => {
  const created = await createCase(request);
  const c = await sendChat(request, created.caseId, "I want to refinance for a lower rate");
  expect(c.stopPath).toBe("WR-LOAN");
  expect(c.status).toBe("stopped");
  await openCaseOnStep(page, created.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Stopped");
});

test("SP-04 refinance and protect the loan does not stop", async ({ request }) => {
  const created = await createCase(request);
  const c = await sendChat(request, created.caseId, "I want to refinance and also protect the loan");
  expect(c.stopPath).toBeFalsy();
  expect(c.status).not.toBe("stopped");
});

test("SP-05 health insurance stops", async ({ request }) => {
  const created = await createCase(request);
  const c = await sendChat(request, created.caseId, "I need help with my ACA health insurance");
  expect(c.stopPath).toBe("WR-HEALTH");
});

test("SP-07 DNC stops", async ({ request }) => {
  const created = await createCase(request);
  const c = await sendChat(request, created.caseId, "Please don't call me again");
  expect(c.stopPath).toBe("DNC");
});

test("S8-E2E-01 handoff ok sets status handoff", async ({ request, page }) => {
  const created = await createCase(request);
  const c = await sendMany(request, created.caseId, [
    STEP_SCRIPT.name,
    STEP_SCRIPT.state,
    STEP_SCRIPT.intent,
    STEP_SCRIPT.age,
    STEP_SCRIPT.loan,
    STEP_SCRIPT.build,
    STEP_SCRIPT.diseases,
    STEP_SCRIPT.income,
  ]);
  expect(c.process.step).toBe(8);
  const done = await sendChat(request, created.caseId, "ok");
  expect(done.profile.agent_handoff_ack).toBe(true);
  expect(done.status).toBe("handoff");
  await openCaseOnStep(page, created.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Hand off to licensed agent");
});
