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

test("普通文案优化标签页可以生成文案结果", async ({ page }) => {
  await page.route("**/api/copy/renwei-optimize", async (route) => {
    const payload = route.request().postDataJSON() as { publish_target?: string };
    expect(payload.publish_target).toBe("朋友圈");
    await route.fulfill({
      status: 200,
      contentType: "application/json",
      body: JSON.stringify({
        case: { id: "case_copy_1", case_type: "human_copy" },
        result_version: { id: "ver_copy_1", version: 1 },
        result: {
          optimized_text: "我观察到大家越来越难进入心流了。",
          change_notes: [
            {
              original: "我观察到大家越来越难进入心流",
              revised: "我观察到大家越来越难进入心流了。",
              reason: "补足句尾语气，让句子自然收住。",
              confidence: "confident",
              revertible: false,
            },
          ],
          ai_taste_checks: [
            {
              rule_id: "promotion_tone",
              status: "pass",
              evidence: "没有新增宣传腔。",
              suggestion: "",
            },
          ],
          warnings: [],
        },
      }),
    });
  });

  await page.goto("/");
  await expect(
    page.getByRole("heading", { name: "GEO 智能文章优化器" }),
  ).toBeVisible();
  await expect(page.getByText("文章输入")).toBeVisible();

  await page.getByRole("button", { name: "普通文案优化" }).click();
  await expect(page.getByLabel("原始文案")).toBeVisible();

  await page.getByLabel("访问密钥").fill("local-dev-key");
  await page.getByLabel("原始文案").fill("我观察到大家越来越难进入心流");
  await page.getByLabel("发布目标").fill("朋友圈");
  await page.getByRole("button", { name: "优化文案" }).click();

  await expect(page.getByText("优化后文案")).toBeVisible();
  await expect(page.locator(".copy-result")).toContainText(
    "我观察到大家越来越难进入心流了。",
  );
  await expect(page.getByRole("link", { name: "查看案例详情" })).toHaveAttribute(
    "href",
    "/cases/case_copy_1",
  );
  await expect(page.getByText("改动说明")).toBeVisible();
  await expect(page.getByText("AI 味检查")).toBeVisible();
});
