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

test("AI-001 AI-003 chat loads and input is usable", async ({ page }) => {
  await waitForChatReady(page);
  const box = page.getByLabel("Chat message");
  await expect(box).toBeEnabled();
  await box.click();
  await box.fill("Hi");
  await expect(box).toHaveValue("Hi");
});

test("AI-004 Send Hi gets a reply and stays on lead intake", async ({ page }) => {
  await waitForChatReady(page);
  await sendUi(page, "Hi");
  await expect(page.getByText("Hi", { exact: true }).first()).toBeVisible();
  await expect(page.getByTestId("process-current")).toHaveText("Lead intake");
});

test("AI-005 Enter submits the chat form", async ({ page }) => {
  await waitForChatReady(page);
  const box = page.getByLabel("Chat message");
  await box.fill("My name is John Smith");
  await box.focus();
  await Promise.all([
    page.waitForResponse(
      (r) => r.url().includes("/api/mortgage/chat") && r.request().method() === "POST",
      { timeout: 45_000 }
    ),
    page.locator("form").evaluate((form) => (form as HTMLFormElement).requestSubmit()),
  ]);
  await expect(page.getByText("John Smith", { exact: true })).toBeVisible();
});

test("AI-006 AI-007 empty and whitespace are not submitted", async ({ page }) => {
  await waitForChatReady(page);
  const users = page.locator("div.ml-8");
  await expect(users).toHaveCount(0);
  await page.getByRole("button", { name: "Send" }).click();
  await expect(users).toHaveCount(0);
  await page.getByLabel("Chat message").fill("   ");
  await page.getByRole("button", { name: "Send" }).click();
  await expect(users).toHaveCount(0);
});

test("AI-009 special characters do not crash chat", async ({ page }) => {
  await waitForChatReady(page);
  await sendUi(page, "!@#$%^&*()");
  await expect(page.getByText("!@#$%^&*()")).toBeVisible();
  await expect(page.getByLabel("Chat message")).toBeEnabled();
});

test("AI-013 single Send shows the user message once", async ({ page }) => {
  await waitForChatReady(page);
  await sendUi(page, "My name is Pat Lee");
  await expect(page.getByText("Pat Lee").first()).toBeVisible();
});

test("AI-056 new session does not inherit other case profile", async ({ request, page }) => {
  const a = await createCase(request);
  await sendChat(request, a.caseId, "My name is Alice Park");
  const b = await createCase(request);
  expect(b.profile.name).toBeFalsy();
  await page.goto(`/mortgage?case=${b.caseId}`);
  await expect(page.getByTestId("process-current")).toHaveText("Lead intake");
  await expect(page.getByText("Alice Park")).toHaveCount(0);
});

test("AI-296 script tag renders as text not alert", async ({ page }) => {
  await waitForChatReady(page);
  page.on("dialog", () => {
    throw new Error("XSS dialog must not appear");
  });
  await sendUi(page, "<script>alert(1)</script>");
  await expect(page.getByText("<script>alert(1)</script>")).toBeVisible();
});

test("AI-016 friendly error on chat failure is not implemented", async ({ page }) => {
  test.fail(true, "ChatPane send() has try/finally and no catch / user error UI");
  await waitForChatReady(page);
  await page.route("**/api/mortgage/chat", (route) => route.abort());
  await page.getByLabel("Chat message").fill("Hi");
  await page.getByRole("button", { name: "Send" }).click();
  await expect(page.getByText(/try again|error|something went wrong/i)).toBeVisible({ timeout: 5_000 });
});
