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

export class DashboardPage {
  readonly page: Page;
  readonly projectSelector: Locator;
  readonly sidebarMenu: Locator;
  readonly logoutButton: Locator;

  constructor(page: Page) {
    this.page = page;
    this.projectSelector = page.locator('.project-selector, [data-testid="project-selector"]');
    this.sidebarMenu = page.locator('.ant-menu, .sidebar-menu');
    this.logoutButton = page.locator('button:has-text("退出"), [data-testid="logout"]');
  }

  async goto() {
    await this.page.goto('/');
    await this.page.waitForLoadState('networkidle');
  }

  async selectProject(projectName: string) {
    await this.projectSelector.click();
    await this.page.locator(`.ant-select-item:has-text("${projectName}")`).click();
    await this.page.waitForLoadState('networkidle');
  }

  async navigateTo(menuItem: string) {
    await this.sidebarMenu.locator(`text=${menuItem}`).click();
    await this.page.waitForLoadState('networkidle');
  }

  async logout() {
    await this.logoutButton.click();
  }

  async isDashboardVisible(): Promise<boolean> {
    return await this.page.locator('.dashboard, [data-testid="dashboard"]').isVisible();
  }
}
