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

test("S0-E2E-01 John Smith in Texas updates profile and progress", async ({ request, page }) => {
  const created = await createCase(request);
  const c = await sendChat(request, created.caseId, "My name is John Smith");
  expect(c.profile.name).toBe("John Smith");
  expect(c.process.step).toBe(0);

  const st = await sendChat(request, created.caseId, "I live in Texas");
  expect(st.profile.state).toBe("TX");
  expect(st.profile.name).toBe("John Smith");
  expect(st.process.step).toBe(1);

  await openCaseOnStep(page, created.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Lock intent");
  await expect(profileRow(page, "name")).toContainText("John Smith");
  await expect(profileRow(page, "state")).toContainText("TX");
});

test("S0-E2E-02 greeting Hi does not set Hawaii or restart", async ({ page }) => {
  await waitForChatReady(page);
  await sendUi(page, "Hi");
  await expect(page.getByTestId("process-current")).toHaveText("Lead intake");
  await expect(profileRow(page, "state")).toContainText("We’ll ask this next.");
  await expect(page.getByText("Hawaii")).toHaveCount(0);
});

test("S1-E2E-01 mortgage protection on me only advances to demographics", async ({ request, page }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, ["My name is John Smith", "Texas"]);
  const c = await sendChat(request, created.caseId, "I want mortgage protection on me only");
  expect(c.profile.intent).toBe("mortgage_protection");
  expect(c.profile.coverage_on_whom).toBe("primary");
  expect(c.process.step).toBe(2);
  await openCaseOnStep(page, created.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Demographics & loan facts");
  await expect(profileRow(page, "coverage_on_whom")).toContainText("primary");
});

test("S2-E2E-01 age and 400k loan do not drop name; step becomes Build", async ({ request, page }) => {
  const created = await createCase(request);
  await sendMany(request, created.caseId, [
    "My name is John Smith",
    "Texas",
    "I want mortgage protection on me only",
  ]);
  await sendChat(request, created.caseId, "I am 42 years old");
  const c = await sendChat(request, created.caseId, "My loan amount is 400k usd on the mortgage");
  expect(c.profile.name).toBe("John Smith");
  expect(c.profile.age).toBe(42);
  expect(c.profile.loan_balance).toBe(400000);
  expect(c.process.step).toBe(3);
  await openCaseOnStep(page, created.caseId);
  await expect(page.getByTestId("process-current")).toHaveText("Build + tobacco");
  await expect(profileRow(page, "loan_balance")).toContainText("400000");
});

test("CHAT-01 user then assistant order after send", async ({ page }) => {
  await waitForChatReady(page);
  await sendUi(page, "My name is John Smith");
  const bubbles = page.locator("section.flex.min-h-0 div.rounded-xl");
  await expect(bubbles.last()).toBeVisible();
  await expect(page.getByText("My name is John Smith")).toBeVisible();
  await expect(page.getByTestId("process-current")).toHaveText("Lead intake");
});
