// apps/web/e2e/flows/appointment-flow.spec.ts
import { test, expect } from '../fixtures/auth.fixture';
import { AppointmentPage } from '../pages/appointment.page';
import { makePatient, makeDoctor, makeSchedule, makeAppointment } from '../fixtures/test-data';

test.describe('@flow 预约排班链路', () => {
  const cleanup: Array<() => Promise<void>> = [];

  test.afterEach(async () => {
    for (const fn of cleanup.reverse()) {
      await fn().catch(() => {});
    }
    cleanup.length = 0;
  });

  test('创建医生 → 设置排班 → 创建预约 → 查看列表', async ({ api, authenticatedPage: page }) => {
    const doctor = await api.createDoctor(makeDoctor());
    cleanup.push(() => api.deleteDoctor(doctor.id, doctor.version));

    const patient = await api.createPatient(makePatient());
    cleanup.push(() => api.deletePatient(patient.id, patient.version));

    const schedule = await api.createSchedule(makeSchedule(doctor.id));
    cleanup.push(() => api.deleteSchedule(schedule.id, schedule.version));

    const appointmentPage = new AppointmentPage(page);
    await appointmentPage.gotoSchedule();

    const appointment = await api.createAppointment(
      makeAppointment(patient.id, doctor.id, schedule.id),
    );
    cleanup.push(() => api.deleteAppointment(appointment.id, appointment.version));

    await appointmentPage.gotoAppointments();
    const tableText = await page.locator('.ant-table-tbody').textContent();
    expect(tableText).toBeTruthy();
  });
});
