import { expect, test } from "@playwright/test";
import { sendUi, waitForChatReady } from "./helpers";

const STEP_LABELS = [
  "0. Lead intake",
  "1. Lock intent",
  "2. Demographics & loan facts",
  "3. Build + tobacco",
  "4. Disease interview",
  "5. Spouse / income",
  "6. Soft decision",
  "7. Carrier shortlist",
  "8. Hand off to licensed agent",
];

test("home, process list 0–8, and independent pane toggles", async ({ page }) => {
  await page.goto("/", { waitUntil: "domcontentloaded" });
  await expect(page.getByRole("heading", { name: /Chat that fills your file/i })).toBeVisible();
  await page.getByRole("link", { name: "Start mortgage chat" }).click();
  await expect(page).toHaveURL(/\/mortgage/);
  await expect(page.getByLabel("Chat message")).toBeEnabled({ timeout: 45_000 });

  for (const label of STEP_LABELS) {
    await expect(page.getByText(label)).toBeVisible();
  }
  await expect(page.getByTestId("process-current")).toHaveText("Lead intake");
  await expect(page.getByTestId("process-exit-gate")).toContainText("Contactable person + state");

  await page.getByRole("button", { name: "Toggle progress" }).click();
  await expect(page.getByRole("heading", { name: "Progress (0–8)" })).toHaveCount(0);
  await expect(page.getByRole("heading", { name: "Applicant profile" })).toBeVisible();
  await page.getByRole("button", { name: "Toggle progress" }).click();

  await page.getByRole("button", { name: "Toggle profile" }).click();
  await expect(page.getByRole("heading", { name: "Applicant profile" })).toHaveCount(0);
  await expect(page.getByLabel("Chat message")).toBeVisible();
});

test("UI advances through process gates to licensed-agent handoff", async ({ page }) => {
  await waitForChatReady(page);
  await expect(page.getByTestId("process-current")).toHaveText("Lead intake");

  await sendUi(page, "My name is Riley Chen");
  await sendUi(page, "Texas");
  await expect(page.getByTestId("process-current")).toHaveText("Lock intent");

  await sendUi(page, "I want mortgage protection on me only");
  await expect(page.getByTestId("process-current")).toHaveText("Demographics & loan facts");

  await sendUi(page, "I am 42 years old");
  await sendUi(page, "loan balance 200k usd, 15 years left");
  await expect(page.getByTestId("process-current")).toHaveText("Build + tobacco");

  await sendUi(page, `5'10" 170 lb non-smoker`);
  await expect(page.getByTestId("process-current")).toHaveText("Disease interview");

  await sendUi(page, "none of those");
  await expect(page.getByTestId("process-current")).toHaveText("Spouse / income");
  await sendUi(page, "my annual income is 85k");
  await expect(page.getByTestId("process-current")).toHaveText("Hand off to licensed agent");

  await sendUi(page, "ok");
  await expect(page.getByText(/licensed agent/i).first()).toBeVisible();
});

test("UI stop path disables chat", 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();
});
