/**
 * healthData 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 { healthDataApi } from './healthData'

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

describe('healthDataApi - Vital Signs', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('listVitalSigns 应调用 GET /health/patients/:id/vital-signs 并传递分页', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await healthDataApi.listVitalSigns('p-001', { page: 1, page_size: 10 })

    expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/vital-signs', {
      params: { page: 1, page_size: 10 },
    })
  })

  it('createVitalSigns 应调用 POST /health/patients/:id/vital-signs', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { record_date: '2026-05-03', systolic_bp_morning: 120, diastolic_bp_morning: 80 }
    await healthDataApi.createVitalSigns('p-001', req)

    expect(mockPost).toHaveBeenCalledWith('/health/patients/p-001/vital-signs', req)
  })

  it('updateVitalSigns 应调用 PUT /health/patients/:pid/vital-signs/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { systolic_bp_morning: 125, version: 1 }
    await healthDataApi.updateVitalSigns('p-001', 'vs-001', req)

    expect(mockPut).toHaveBeenCalledWith('/health/patients/p-001/vital-signs/vs-001', req)
  })

  it('deleteVitalSigns 应调用 DELETE /health/patients/:pid/vital-signs/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await healthDataApi.deleteVitalSigns('p-001', 'vs-001')

    expect(mockDelete).toHaveBeenCalledWith('/health/patients/p-001/vital-signs/vs-001')
  })
})

describe('healthDataApi - Lab Reports', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('listLabReports 应调用 GET /health/patients/:id/lab-reports', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await healthDataApi.listLabReports('p-001', { page: 1, page_size: 10 })

    expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/lab-reports', {
      params: { page: 1, page_size: 10 },
    })
  })

  it('createLabReport 应调用 POST /health/patients/:id/lab-reports', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { report_date: '2026-05-03', report_type: 'blood_test' }
    await healthDataApi.createLabReport('p-001', req)

    expect(mockPost).toHaveBeenCalledWith('/health/patients/p-001/lab-reports', req)
  })

  it('reviewLabReport 应调用 PUT /health/patients/:pid/lab-reports/:rid/review', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { version: 1, doctor_notes: '指标正常' }
    await healthDataApi.reviewLabReport('p-001', 'lr-001', req)

    expect(mockPut).toHaveBeenCalledWith('/health/patients/p-001/lab-reports/lr-001/review', req)
  })

  it('deleteLabReport 应调用 DELETE /health/patients/:pid/lab-reports/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await healthDataApi.deleteLabReport('p-001', 'lr-001')

    expect(mockDelete).toHaveBeenCalledWith('/health/patients/p-001/lab-reports/lr-001')
  })
})

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

  it('listHealthRecords 应调用 GET /health/patients/:id/health-records', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await healthDataApi.listHealthRecords('p-001', { page: 1, page_size: 10 })

    expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/health-records', {
      params: { page: 1, page_size: 10 },
    })
  })

  it('createHealthRecord 应调用 POST /health/patients/:id/health-records', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { record_type: 'checkup', record_date: '2026-05-03', content: '体检结果正常' }
    await healthDataApi.createHealthRecord('p-001', req)

    expect(mockPost).toHaveBeenCalledWith('/health/patients/p-001/health-records', req)
  })
})

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

  it('listTrends 应调用 GET /health/patients/:id/trends', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await healthDataApi.listTrends('p-001')

    expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/trends')
  })

  it('getIndicatorTimeseries 应调用 GET /health/patients/:id/trends/:indicator 并编码', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await healthDataApi.getIndicatorTimeseries('p-001', 'blood_pressure/systolic')

    expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/trends/blood_pressure%2Fsystolic')
  })
})
