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

export class HealthScorePage {
  readonly page: Page;
  readonly brandInput: Locator;
  readonly checkButton: Locator;
  readonly scoreDisplay: Locator;
  readonly healthLevelBadge: Locator;
  readonly dimensionList: Locator;
  readonly registerButton: Locator;
  readonly errorMessage: Locator;
  readonly loadingSpinner: Locator;

  constructor(page: Page) {
    this.page = page;
    this.brandInput = page.locator('input[placeholder*="品牌"]');
    this.checkButton = page.getByRole("button", { name: /检测/ });
    this.scoreDisplay = page.locator("text=/\\d+/100/").first();
    this.healthLevelBadge = page.locator("[data-slot='badge']").first();
    this.dimensionList = page.locator("text=核心维度评分");
    this.registerButton = page.getByRole("button", { name: /注册/ });
    this.errorMessage = page.locator(".text-destructive");
    this.loadingSpinner = page.locator(".animate-spin");
  }

  async goto() {
    await this.page.goto("/health-score");
    await this.page.waitForLoadState("domcontentloaded");
  }

  async checkBrand(brandName: string) {
    await this.brandInput.fill(brandName);
    await this.checkButton.click();
  }

  async waitForResults(timeout = 30000) {
    await this.page.waitForURL(/health-score/, { timeout });
    await expect(this.scoreDisplay).toBeVisible({ timeout });
  }
}
