/**
 * followUp 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 { followUpApi } from './followUp'

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

describe('followUpApi - Tasks', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('listTasks 应调用 GET /health/follow-up-tasks 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await followUpApi.listTasks({ page: 1, page_size: 20, patient_id: 'p-001', status: 'pending' })

    expect(mockGet).toHaveBeenCalledWith('/health/follow-up-tasks', {
      params: { page: 1, page_size: 20, patient_id: 'p-001', status: 'pending' },
    })
  })

  it('getTask 应调用 GET /health/follow-up-tasks/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await followUpApi.getTask('task-001')

    expect(mockGet).toHaveBeenCalledWith('/health/follow-up-tasks/task-001')
  })

  it('createTask 应调用 POST /health/follow-up-tasks', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { patient_id: 'p-001', follow_up_type: 'phone', planned_date: '2026-05-10' }
    await followUpApi.createTask(req)

    expect(mockPost).toHaveBeenCalledWith('/health/follow-up-tasks', req)
  })

  it('updateTask 应调用 PUT /health/follow-up-tasks/:id 并传递 version', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { status: 'completed', version: 1 }
    await followUpApi.updateTask('task-001', req)

    expect(mockPut).toHaveBeenCalledWith('/health/follow-up-tasks/task-001', req)
  })

  it('deleteTask 应调用 DELETE /health/follow-up-tasks/:id 并在 body 传递 version', async () => {
    mockDelete.mockResolvedValue(undefined)
    await followUpApi.deleteTask('task-001', 2)

    expect(mockDelete).toHaveBeenCalledWith('/health/follow-up-tasks/task-001', {
      data: { version: 2 },
    })
  })
})

describe('followUpApi - Records', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('listRecords 应调用 GET /health/follow-up-records 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await followUpApi.listRecords({ page: 1, page_size: 10, task_id: 'task-001' })

    expect(mockGet).toHaveBeenCalledWith('/health/follow-up-records', {
      params: { page: 1, page_size: 10, task_id: 'task-001' },
    })
  })

  it('createRecord 应调用 POST /health/follow-up-tasks/:taskId/records 并注入 task_id', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = {
      executed_date: '2026-05-10',
      result: '已完成',
      patient_condition: '良好',
    }
    await followUpApi.createRecord('task-001', req)

    expect(mockPost).toHaveBeenCalledWith('/health/follow-up-tasks/task-001/records', {
      ...req,
      task_id: 'task-001',
    })
  })
})
