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

export class DashboardPage {
  readonly page: Page;
  readonly pageTitle: Locator;
  readonly healthScoreCard: Locator;
  readonly competitorStatusCard: Locator;
  readonly trendCard: Locator;
  readonly monitorPlatformCard: Locator;
  readonly platformScoreList: Locator;
  readonly actionSuggestions: Locator;

  constructor(page: Page) {
    this.page = page;
    // 品牌健康中心标题
    this.pageTitle = page.getByRole("heading", { name: "品牌健康中心" });
    // 概览卡片
    this.healthScoreCard = page.locator("text=综合评分").first();
    this.competitorStatusCard = page.locator("text=竞品地位").first();
    this.trendCard = page.locator("text=趋势").first();
    this.monitorPlatformCard = page.locator("text=监控平台").first();
    // 平台评分列表
    this.platformScoreList = page.locator("text=平台评分详情");
    // 行动建议
    this.actionSuggestions = page.locator("text=为您推荐的下一步行动");
  }

  async goto() {
    await this.page.goto("/dashboard");
  }

  async expectToBeVisible() {
    await expect(this.pageTitle).toBeVisible();
  }

  // 等待健康状态卡片加载
  async waitForHealthCards() {
    await expect(this.healthScoreCard).toBeVisible({ timeout: 10000 });
  }

  // 获取综合评分值
  async getOverallScore(): Promise<string | null> {
    const scoreElement = this.page.locator(".text-5xl.font-bold").first();
    if (await scoreElement.isVisible()) {
      return scoreElement.textContent();
    }
    return null;
  }

  // 获取健康等级标签
  async getHealthLevel(): Promise<Locator> {
    return this.page.locator(".rounded-full.px-3.py-1.text-sm.font-medium").first();
  }

  // 获取平台评分项数量
  async getPlatformScoreCount(): Promise<number> {
    const items = this.page.locator("text=/文心一言|Kimi|通义千问|豆包|讯飞星火|天工AI|智谱清言/");
    return items.count();
  }

  // 获取行动建议数量
  async getActionSuggestionCount(): Promise<number> {
    const suggestions = this.page.locator(".rounded-lg.border p-4");
    return suggestions.count();
  }

  // 检查危险平台是否被高亮
  async isDangerPlatformHighlighted(): Promise<boolean> {
    const dangerCard = this.page.locator(".border-red-200.bg-red-50\\/50");
    const count = await dangerCard.count();
    return count > 0;
  }
}
