import { expect, test } from "@playwright/test";

import {
  THREAD_FOR_WELCOME,
  invalidNewChatUrl,
  newChatEntry,
  openChat,
  reuseThreadChatEntry,
  reuseThreadWelcomeEntry,
  skipIfMissingThread,
  waitForMessageListReady,
} from "./support/chat-helpers";

test.describe("聊天工作台 / 路由与欢迎态", () => {
  test("DF-ROUTE-001 /new 带 thread_id 时展示欢迎态与建议词", async ({
    page,
  }, testInfo) => {
    skipIfMissingThread(testInfo, THREAD_FOR_WELCOME, "FRONTEND_E2E_THREAD_ID");
    await openChat(page, newChatEntry(THREAD_FOR_WELCOME!));

    const suggestions = page.getByTestId("welcome-suggestions");
    await expect(suggestions).toBeVisible();
    await expect(suggestions.locator("button").first()).toBeVisible();
    await expect(page.locator(".is-user, .is-assistant")).toHaveCount(0);
  });

  test("DF-ROUTE-002 /new 复用模式保留欢迎态且不直接渲染历史", async ({
    page,
  }, testInfo) => {
    skipIfMissingThread(testInfo, THREAD_FOR_WELCOME, "FRONTEND_E2E_THREAD_ID");
    await openChat(page, reuseThreadWelcomeEntry(THREAD_FOR_WELCOME!));

    await expect(page).toHaveURL(
      new RegExp(`/workspace/chats/new\\?.*thread_id=${THREAD_FOR_WELCOME!}`),
    );
    await expect(page.getByTestId("welcome-suggestions")).toBeVisible();
    await expect(page.locator(".is-user, .is-assistant")).toHaveCount(0);
  });

  test("DF-ROUTE-003 /new 缺少 thread_id 时进入欢迎态", async ({ page }) => {
    await page.goto(invalidNewChatUrl());

    await expect(page.getByTestId("welcome-suggestions")).toBeVisible();
    await expect(page.locator("textarea[name='message']")).toBeVisible();
  });

  test("DF-ROUTE-004 线程页直接展示历史消息与标题栏", async ({
    page,
  }, testInfo) => {
    skipIfMissingThread(testInfo, THREAD_FOR_WELCOME, "FRONTEND_E2E_THREAD_ID");
    await openChat(page, reuseThreadChatEntry(THREAD_FOR_WELCOME!));
    await waitForMessageListReady(page);

    await expect(page).toHaveURL(
      new RegExp(`/workspace/chats/${THREAD_FOR_WELCOME!}`),
    );
    await expect(page.getByRole("log").first()).toBeVisible();
    await expect(page.locator("header button").first()).toBeVisible();
    await expect(page.getByTestId("welcome-suggestions")).toHaveCount(0);
  });

  test("DF-ROUTE-005 退出确认取消后保持当前线程页面", async ({
    page,
  }, testInfo) => {
    skipIfMissingThread(testInfo, THREAD_FOR_WELCOME, "FRONTEND_E2E_THREAD_ID");
    await openChat(page, reuseThreadChatEntry(THREAD_FOR_WELCOME!));
    await waitForMessageListReady(page);

    await page.locator("header button").first().click();
    await expect(page.getByText("退出后，当前会话结束并销毁")).toBeVisible();
    await page.getByRole("button", { name: "取消" }).click();

    await expect(page).toHaveURL(
      new RegExp(`/workspace/chats/${THREAD_FOR_WELCOME!}`),
    );
  });

  test("DF-ROUTE-006 退出确认确认后返回带 thread_id 的 /new", async ({
    page,
  }, testInfo) => {
    skipIfMissingThread(testInfo, THREAD_FOR_WELCOME, "FRONTEND_E2E_THREAD_ID");
    await openChat(page, reuseThreadChatEntry(THREAD_FOR_WELCOME!));
    await waitForMessageListReady(page);

    await page.locator("header button").first().click();
    await page.getByRole("button", { name: "确定" }).click();

    await expect(page).toHaveURL(
      new RegExp(`/workspace/chats/new\\?.*thread_id=${THREAD_FOR_WELCOME!}`),
    );
    await expect(page.getByTestId("welcome-suggestions")).toBeVisible();
  });
});
