// apps/web/e2e/pages/health-data.page.ts
import type { Page, Locator } from '@playwright/test';

export class HealthDataPage {
  readonly page: Page;

  constructor(page: Page) {
    this.page = page;
  }

  async clickAddVitalSigns() {
    await this.page.click('button:has-text("录入体征"), button:has-text("新增")');
    await this.page.waitForSelector('.ant-modal', { timeout: 5000 });
  }

  private formField(labelText: string): Locator {
    const modal = this.page.locator('.ant-modal');
    return modal.locator('.ant-form-item').filter({ hasText: labelText }).locator('input');
  }

  async fillVitalSignsForm(data: {
    record_date?: string;
    systolic_bp_morning?: number;
    diastolic_bp_morning?: number;
    heart_rate?: number;
    body_temperature?: number;
    spo2?: number;
    weight?: number;
    blood_sugar?: number;
  }) {
    const modal = this.page.locator('.ant-modal');

    // Fill date - DatePicker needs special handling
    const dateToFill = data.record_date || new Date().toISOString().slice(0, 10);
    const datePicker = modal.locator('.ant-form-item').filter({ hasText: '记录日期' }).locator('input');
    await datePicker.click();
    await datePicker.fill(dateToFill);
    await this.page.keyboard.press('Enter');
    await this.page.waitForTimeout(300);

    if (data.systolic_bp_morning) {
      await this.formField('收缩压(晨)').fill(String(data.systolic_bp_morning));
    }
    if (data.diastolic_bp_morning) {
      await this.formField('舒张压(晨)').fill(String(data.diastolic_bp_morning));
    }
    if (data.heart_rate) {
      await this.formField('心率').fill(String(data.heart_rate));
    }
    if (data.weight) {
      await this.formField('体重').fill(String(data.weight));
    }
    if (data.blood_sugar) {
      await this.formField('血糖').fill(String(data.blood_sugar));
    }
  }

  async submitVitalSigns() {
    const modal = this.page.locator('.ant-modal');
    await modal.locator('.ant-modal-footer button.ant-btn-primary').click();
    await this.page.waitForSelector('.ant-message-success', { timeout: 10000 });
  }

  async getVitalSignsList(): Promise<string[]> {
    const rows = this.page.locator('.ant-table-tbody tr');
    const count = await rows.count();
    const texts: string[] = [];
    for (let i = 0; i < count; i++) {
      texts.push(await rows.nth(i).textContent() ?? '');
    }
    return texts;
  }

  async trendChartIsVisible(): Promise<boolean> {
    const chart = this.page.locator('canvas, .recharts-wrapper, [class*="chart"]');
    return chart.isVisible();
  }
}
