/**
 * followUpTemplates 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 { followUpTemplateApi } from './followUpTemplates'

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

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

  it('list 应调用 GET /health/follow-up-templates 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await followUpTemplateApi.list({ page: 1, page_size: 10, follow_up_type: 'phone', status: 'active' })

    expect(mockGet).toHaveBeenCalledWith('/health/follow-up-templates', {
      params: { page: 1, page_size: 10, follow_up_type: 'phone', status: 'active' },
    })
  })

  it('get 应调用 GET /health/follow-up-templates/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await followUpTemplateApi.get('tpl-001')

    expect(mockGet).toHaveBeenCalledWith('/health/follow-up-templates/tpl-001')
  })

  it('create 应调用 POST /health/follow-up-templates 并传递请求体', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = {
      name: '电话随访模板',
      follow_up_type: 'phone',
      fields: [
        { label: '患者状态', field_key: 'patient_status', field_type: 'select', required: true, options: '良好,一般,较差' },
      ],
    }
    await followUpTemplateApi.create(req)

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

  it('update 应调用 PUT /health/follow-up-templates/:id 并传递请求体', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { name: '更新后模板', status: 'active', version: 1 }
    await followUpTemplateApi.update('tpl-001', req)

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

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

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