/**
 * appointments API 契约测试
 */
import { describe, it, expect, vi, beforeEach } from 'vitest'

const mockGet = vi.fn()
const mockPost = vi.fn()
const mockPut = vi.fn()
const mockDelete = vi.fn()

vi.mock('../client', () => ({
  default: {
    get: (...args: unknown[]) => mockGet(...args),
    post: (...args: unknown[]) => mockPost(...args),
    put: (...args: unknown[]) => mockPut(...args),
    delete: (...args: unknown[]) => mockDelete(...args),
  },
}))

import { appointmentApi } from './appointments'

beforeEach(() => {
  vi.clearAllMocks()
})

describe('appointmentApi', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('list 应调用 GET /health/appointments 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await appointmentApi.list({ page: 1, page_size: 20, status: 'confirmed', doctor_id: 'd-001' })

    expect(mockGet).toHaveBeenCalledWith('/health/appointments', {
      params: { page: 1, page_size: 20, status: 'confirmed', doctor_id: 'd-001' },
    })
  })

  it('get 应调用 GET /health/appointments/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await appointmentApi.get('appt-001')

    expect(mockGet).toHaveBeenCalledWith('/health/appointments/appt-001')
  })

  it('create 应调用 POST /health/appointments 并传递请求体', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = {
      patient_id: 'p-001',
      doctor_id: 'd-001',
      appointment_date: '2026-05-10',
      start_time: '09:00',
      end_time: '09:30',
    }
    await appointmentApi.create(req)

    expect(mockPost).toHaveBeenCalledWith('/health/appointments', req)
  })

  it('updateStatus 应调用 PUT /health/appointments/:id/status', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { status: 'cancelled', cancel_reason: '时间冲突', version: 2 }
    await appointmentApi.updateStatus('appt-001', req)

    expect(mockPut).toHaveBeenCalledWith('/health/appointments/appt-001/status', req)
  })

  it('listSchedules 应调用 GET /health/doctor-schedules 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await appointmentApi.listSchedules({ doctor_id: 'd-001', date: '2026-05-10' })

    expect(mockGet).toHaveBeenCalledWith('/health/doctor-schedules', {
      params: { doctor_id: 'd-001', date: '2026-05-10' },
    })
  })

  it('createSchedule 应调用 POST /health/doctor-schedules', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = {
      doctor_id: 'd-001',
      schedule_date: '2026-05-10',
      start_time: '08:00',
      end_time: '12:00',
      max_appointments: 10,
    }
    await appointmentApi.createSchedule(req)

    expect(mockPost).toHaveBeenCalledWith('/health/doctor-schedules', req)
  })

  it('updateSchedule 应调用 PUT /health/doctor-schedules/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { max_appointments: 15, version: 1 }
    await appointmentApi.updateSchedule('sch-001', req)

    expect(mockPut).toHaveBeenCalledWith('/health/doctor-schedules/sch-001', req)
  })

  it('calendar 应调用 GET /health/doctor-schedules/calendar', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await appointmentApi.calendar({ start_date: '2026-05-01', end_date: '2026-05-31', doctor_id: 'd-001' })

    expect(mockGet).toHaveBeenCalledWith('/health/doctor-schedules/calendar', {
      params: { start_date: '2026-05-01', end_date: '2026-05-31', doctor_id: 'd-001' },
    })
  })
})
