// apps/web/e2e/pages/appointment.page.ts
import type { Page } from '@playwright/test';

export class AppointmentPage {
  readonly page: Page;

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

  async gotoSchedule() {
    await this.page.goto('/#/health/schedules');
    await this.page.waitForSelector('.ant-table, .ant-fullcalendar, [class*="calendar"]', { timeout: 10000 });
  }

  async gotoAppointments() {
    await this.page.goto('/#/health/appointments');
    await this.page.waitForSelector('.ant-table', { timeout: 10000 });
  }

  async clickCreateSchedule() {
    await this.page.click('button:has-text("新增排班"), button:has-text("创建")');
    await this.page.waitForSelector('.ant-modal, .ant-drawer', { timeout: 5000 });
  }

  async fillScheduleForm(data: { doctor_id?: string; date: string; start_time: string; end_time: string }) {
    if (data.doctor_id) {
      await this.page.click('.ant-select');
      await this.page.click('.ant-select-item-option');
    }
    await this.page.fill('input[placeholder*="日期"]', data.date);
    await this.page.fill('input[placeholder*="开始"]', data.start_time);
    await this.page.fill('input[placeholder*="结束"]', data.end_time);
  }

  async submitScheduleForm() {
    await this.page.click('.ant-modal button[type="submit"], .ant-btn-primary');
    await this.page.waitForSelector('.ant-message-success', { timeout: 10000 });
  }

  async clickCreateAppointment() {
    await this.page.click('button:has-text("新增预约"), button:has-text("创建")');
    await this.page.waitForSelector('.ant-modal, .ant-drawer', { timeout: 5000 });
  }

  async fillAppointmentForm(data: { patient_id: string; doctor_id: string; date: string; reason?: string }) {
    if (data.reason) {
      await this.page.fill('textarea, input[placeholder*="原因"]', data.reason);
    }
  }

  async submitAppointmentForm() {
    await this.page.click('.ant-modal button[type="submit"], .ant-btn-primary');
    await this.page.waitForSelector('.ant-message-success', { timeout: 10000 });
  }
}
