const { expect, test } = require("@playwright/test");

const marketKeys = [
  "emotion", "pool", "broken", "limit-down", "yesterday", "performance",
  "ladder", "rotation", "auction", "themes", "popularity", "dragon-list",
];

const summary = {
  context: {
    requested_date: "2026-07-30", actual_date: "2026-07-30", previous_date: "2026-07-29",
    observed_at: "2026-07-30T15:00:00+08:00", state: "final", carried_forward: false, message: "",
  },
  values: { temperature: 42, limit_up: 68, limit_down: 4, broken: 24, seal_rate: 73.9, amount: 1628000000000 },
};

async function authenticate(page) {
  await page.goto("/");
  await page.getByLabel("账号名").fill("stage4admin");
  await page.getByLabel("密码").fill("Stage4-pass-123!");
  await page.getByRole("button", { name: "登录", exact: true }).click();
  await expect(page.locator(".sidebar")).toBeVisible();
}

async function delay(milliseconds = 180) {
  await new Promise((resolve) => setTimeout(resolve, milliseconds));
}

async function baseRoutes(page) {
  await page.route("**/api/market/summary", (route) => route.fulfill({
    contentType: "application/json", body: JSON.stringify(summary),
  }));
  await page.route("**/api/review/alerts?*", (route) => route.fulfill({
    contentType: "application/json", body: JSON.stringify({ items: [], unread_count: 0, as_of: "2026-07-30" }),
  }));
}

test("all market workspaces expose loading, empty and failure truthfully", async ({ page }) => {
  let state = "empty";
  await baseRoutes(page);
  const respond = async (route) => {
    if (state === "loading-empty") await delay();
    if (state === "failure") return route.fulfill({ status: 503, contentType: "application/json", body: JSON.stringify({ message: "矩阵测试上游失败" }) });
    return route.fulfill({ contentType: "application/json", body: JSON.stringify({ trade_date: "", message: "等待管理员首次同步真实收盘行情" }) });
  };
  await page.route("**/api/market/workspaces/*", respond);
  await page.route("**/api/market/insights/*", respond);
  await authenticate(page);

  for (const key of marketKeys) {
    state = "loading-empty";
    await page.goto(`/workspace/${key}`);
    await expect(page.getByText("正在读取本地复盘数据", { exact: true }), `loading ${key}`).toBeVisible();
    await expect(page.getByRole("heading", { name: "暂无真实行情快照" }), `empty ${key}`).toBeVisible();

    state = "failure";
    await page.reload({ waitUntil: "domcontentloaded" });
    await expect(page.getByRole("heading", { name: "页面数据暂不可用" }), `failure ${key}`).toBeVisible();
    await expect(page.getByText("请求失败，请稍后重试。", { exact: true }), `failure reason ${key}`).toBeVisible();
  }
});

test("smart and private workspaces expose loading, empty and failure truthfully", async ({ page }) => {
  let state = "empty";
  await baseRoutes(page);

  const response = async (route, payload) => {
    if (state === "loading-empty") await delay();
    if (state === "failure") return route.fulfill({ status: 503, contentType: "application/json", body: JSON.stringify({ message: "矩阵测试服务失败" }) });
    return route.fulfill({ contentType: "application/json", body: JSON.stringify(payload) });
  };
  const catalog = { factor_groups: {}, factors: {}, stage: [], curated: [] };
  const screener = { trade_date: "2026-07-30", message: "", catalog, stage_runs: [], curated_runs: [], custom_strategies: [], custom_runs: [] };
  const review = { trade_date: "2026-07-30", watchlist: [], daily: null, history: [], trades: [], trade_summary: { total: 0, realized: 0, win_rate: null, pnl_amount: null, average_position: null } };
  await page.route("**/api/screener/catalog", (route) => response(route, catalog));
  await page.route("**/api/screener?*", (route) => response(route, screener));
  await page.route("**/api/screener/tracks", (route) => response(route, []));
  await page.route("**/api/mentors/setup?*", (route) => response(route, { trade_date: "2026-07-30", mentors: [] }));
  await page.route("**/api/heaven/setup?*", (route) => response(route, { date: "2026-07-30", fortune: {}, daily_fortune: null, history: [] }));
  await page.route("**/api/review?*", (route) => response(route, review));
  await authenticate(page);

  const cases = [
    { path: "/workspace/screener", key: "screener", loading: "正在读取本地选股归档", empty: "等待盘后判定", failure: "智能选股暂不可用" },
    { path: "/workspace/screener/tracking", key: "tracking", loading: "正在读取跟踪记录", empty: "暂无跟踪记录", failure: "跟踪记录暂不可用" },
    { path: "/workspace/mentor", key: "mentor", loading: "正在读取思维模型", empty: "从问题出发，按模型的方法拆解市场。", failure: "问师暂不可用" },
    { path: "/workspace/heaven", key: "heaven", loading: "正在推演当日基础气机", empty: "请输入股票代码或股票名称", failure: "请求失败，请稍后重试。" },
    { path: "/workspace/review", key: "review", loading: "正在读取个人复盘记录", empty: "暂无自选", failure: "复盘记录暂不可用" },
  ];

  for (const item of cases) {
    state = "loading-empty";
    await page.goto(item.path);
    await expect(page.getByText(item.loading, { exact: true }), `loading ${item.key}`).toBeVisible();
    await expect(page.getByText(item.empty, { exact: true }).first(), `empty ${item.key}`).toBeVisible();

    state = "failure";
    await page.reload({ waitUntil: "domcontentloaded" });
    await expect(page.getByText(item.failure, { exact: true }).first(), `failure ${item.key}`).toBeVisible();
    await expect(page.getByText("请求失败，请稍后重试。", { exact: true }).first(), `failure reason ${item.key}`).toBeVisible();
  }
});

test("alerts and review assistant distinguish loading, empty and failure", async ({ page }) => {
  let state = "empty";
  await baseRoutes(page);
  const response = async (route, payload) => {
    if (state === "loading-empty") await delay();
    if (state === "failure") return route.fulfill({ status: 503, contentType: "application/json", body: JSON.stringify({ message: "不应暴露的上游正文" }) });
    return route.fulfill({ contentType: "application/json", body: JSON.stringify(payload) });
  };
  await page.route("**/api/review/alerts?*", (route) => response(route, { items: [], unread_count: 0, as_of: "2026-07-30" }));
  await page.route("**/api/review/assistant/messages", (route) => response(route, []));
  await authenticate(page);

  state = "loading-empty";
  await page.getByRole("button", { name: "提醒中心" }).click();
  let dialog = page.getByRole("dialog", { name: "提醒中心" });
  await expect(dialog.getByText("正在读取提醒", { exact: true })).toBeVisible();
  await expect(dialog.getByRole("heading", { name: "暂无提醒" })).toBeVisible();
  await dialog.getByLabel("关闭").click();
  state = "failure";
  await page.getByRole("button", { name: "提醒中心" }).click();
  dialog = page.getByRole("dialog", { name: "提醒中心" });
  await expect(dialog.getByRole("heading", { name: "提醒暂不可用" })).toBeVisible();
  await expect(dialog.getByText("请求失败，请稍后重试。", { exact: true })).toBeVisible();
  await dialog.getByLabel("关闭").click();

  state = "loading-empty";
  await page.getByRole("button", { name: "复盘助手" }).click();
  dialog = page.getByRole("dialog", { name: "复盘助手" });
  await expect(dialog.getByText("正在读取复盘助手对话", { exact: true })).toBeVisible();
  await expect(dialog.getByText("可以从市场、策略或自己的交易记录开始复盘", { exact: true })).toBeVisible();
  await dialog.getByLabel("关闭").click();
  state = "failure";
  await page.getByRole("button", { name: "复盘助手" }).click();
  dialog = page.getByRole("dialog", { name: "复盘助手" });
  await expect(dialog.getByText("复盘助手对话暂不可用", { exact: true })).toBeVisible();
  await expect(dialog.getByText("请求失败，请稍后重试。", { exact: true })).toBeVisible();
});

test("system model and membership panels distinguish loading, empty and failure", async ({ page }) => {
  let state = "empty";
  await baseRoutes(page);
  const response = async (route, payload) => {
    if (state === "loading-empty") await delay();
    if (state === "failure") return route.fulfill({ status: 503, contentType: "application/json", body: JSON.stringify({ message: "不应暴露的系统错误" }) });
    return route.fulfill({ contentType: "application/json", body: JSON.stringify(payload) });
  };
  await page.route("**/api/admin/system/credentials", (route) => route.fulfill({ contentType: "application/json", body: "[]" }));
  await page.route("**/api/admin/operations/jobs?*", (route) => route.fulfill({ contentType: "application/json", body: "[]" }));
  await page.route("**/api/admin/models", (route) => response(route, []));
  await page.route("**/api/admin/memberships", (route) => response(route, []));
  await authenticate(page);
  await page.goto("/system");

  state = "loading-empty";
  await page.getByRole("button", { name: "模型池", exact: true }).click();
  await expect(page.getByText("正在读取模型池", { exact: true })).toBeVisible();
  await expect(page.getByText("尚未添加模型", { exact: true })).toBeVisible();
  state = "failure";
  await page.getByRole("button", { name: "行情管理", exact: true }).click();
  await page.getByRole("button", { name: "模型池", exact: true }).click();
  await expect(page.getByText("请求失败，请稍后重试。", { exact: true }).first()).toBeVisible();

  state = "loading-empty";
  await page.getByRole("button", { name: "会员管理", exact: true }).click();
  await expect(page.getByText("正在读取会员列表", { exact: true })).toBeVisible();
  await expect(page.getByText("请选择账号", { exact: true })).toBeVisible();
  state = "failure";
  await page.getByRole("button", { name: "行情管理", exact: true }).click();
  await page.getByRole("button", { name: "会员管理", exact: true }).click();
  await expect(page.getByText("请求失败，请稍后重试。", { exact: true }).first()).toBeVisible();
});

test("account profile and membership dialogs distinguish loading, inactive and failure", async ({ page }) => {
  let state = "empty";
  await baseRoutes(page);
  const response = async (route, payload) => {
    if (state === "loading-empty") await delay();
    if (state === "failure") return route.fulfill({ status: 503, contentType: "application/json", body: JSON.stringify({ message: "不应暴露的账户错误" }) });
    return route.fulfill({ contentType: "application/json", body: JSON.stringify(payload) });
  };
  await page.route("**/api/account/profile", (route) => response(route, { configured: false, birth_date: null, birth_time: null, gender: null, privacy_notice: "原始出生资料加密保存，仅当前账号可见。" }));
  await page.route("**/api/account/membership", (route) => response(route, { status: "none", active: false, is_permanent: false, expires_at: null, remaining_days: null, daily_limit: 50, used_today: 0, remaining_today: 50, quota_exempt: false, smart_access: true, description: "开通会员后可使用智能选股、问师、问天、复盘助手等智能功能。", usage_package_available: false }));
  await authenticate(page);

  const openAccountItem = async (name) => {
    await page.getByRole("button", { name: /stage4admin/ }).click();
    await page.locator(".account-menu").getByRole("button", { name, exact: true }).click();
  };

  state = "loading-empty";
  await openAccountItem("个人资料");
  let dialog = page.getByRole("dialog", { name: "个人资料" });
  await expect(dialog.getByText("正在读取个人资料", { exact: true })).toBeVisible();
  await expect(dialog.getByLabel("出生日期")).toHaveValue("");
  await dialog.getByLabel("关闭").click();
  state = "failure";
  await openAccountItem("个人资料");
  dialog = page.getByRole("dialog", { name: "个人资料" });
  await expect(dialog.getByRole("alert")).toHaveText("请求失败，请稍后重试。");
  await dialog.getByLabel("关闭").click();

  state = "loading-empty";
  await openAccountItem("会员状态");
  dialog = page.getByRole("dialog", { name: "会员状态" });
  await expect(dialog.getByText("正在读取会员状态", { exact: true })).toBeVisible();
  await expect(dialog.getByText("未开通", { exact: true }).first()).toBeVisible();
  await dialog.getByLabel("关闭").click();
  state = "failure";
  await openAccountItem("会员状态");
  dialog = page.getByRole("dialog", { name: "会员状态" });
  await expect(dialog.getByRole("alert")).toHaveText("请求失败，请稍后重试。");
});
