/**
 * consultations 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 { consultationApi } from './consultations'

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

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

  it('listSessions 应调用 GET /health/consultation-sessions 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await consultationApi.listSessions({ page: 1, page_size: 20, status: 'active', patient_id: 'p-001' })

    expect(mockGet).toHaveBeenCalledWith('/health/consultation-sessions', {
      params: { page: 1, page_size: 20, status: 'active', patient_id: 'p-001' },
    })
  })

  it('createSession 应调用 POST /health/consultation-sessions', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { patient_id: 'p-001', doctor_id: 'd-001', consultation_type: 'online' }
    await consultationApi.createSession(req)

    expect(mockPost).toHaveBeenCalledWith('/health/consultation-sessions', req)
  })

  it('getSession 应调用 GET /health/consultation-sessions/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await consultationApi.getSession('sess-001')

    expect(mockGet).toHaveBeenCalledWith('/health/consultation-sessions/sess-001')
  })

  it('closeSession 应调用 PUT /health/consultation-sessions/:id/close', async () => {
    mockPut.mockResolvedValue(fakeRes)
    await consultationApi.closeSession('sess-001', { version: 1 })

    expect(mockPut).toHaveBeenCalledWith('/health/consultation-sessions/sess-001/close', { version: 1 })
  })

  it('listMessages 应调用 GET /health/consultation-sessions/:id/messages 并传递分页参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await consultationApi.listMessages('sess-001', { page: 2, page_size: 50, after_id: 'msg-100' })

    expect(mockGet).toHaveBeenCalledWith('/health/consultation-sessions/sess-001/messages', {
      params: { page: 2, page_size: 50, after_id: 'msg-100' },
    })
  })

  it('createMessage 应调用 POST /health/consultation-messages', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { session_id: 'sess-001', content_type: 'text', content: '你好' }
    await consultationApi.createMessage(req)

    expect(mockPost).toHaveBeenCalledWith('/health/consultation-messages', req)
  })

  it('createFollowUpFromSession 应调用 POST /health/consultation-sessions/:id/follow-up', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { follow_up_type: 'phone', planned_date: '2026-06-01' }
    await consultationApi.createFollowUpFromSession('sess-001', req)

    expect(mockPost).toHaveBeenCalledWith('/health/consultation-sessions/sess-001/follow-up', req)
  })

  it('triggerAiAnalysisFromSession 应调用 POST /health/consultation-sessions/:id/ai-analysis', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await consultationApi.triggerAiAnalysisFromSession('sess-001')

    expect(mockPost).toHaveBeenCalledWith('/health/consultation-sessions/sess-001/ai-analysis', {})
  })

  it('triggerAiAnalysisFromSession 传入 analysis_type 时应携带参数', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await consultationApi.triggerAiAnalysisFromSession('sess-001', { analysis_type: 'trend' })

    expect(mockPost).toHaveBeenCalledWith('/health/consultation-sessions/sess-001/ai-analysis', { analysis_type: 'trend' })
  })
})
