/**
 * alerts 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 { alertApi, alertRuleApi } from './alerts'

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

describe('alertApi', () => {
  const fakeRes = { data: { data: {} } }

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

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

  it('acknowledge 应调用 PUT /health/alerts/:id/acknowledge 并传递 version', async () => {
    mockPut.mockResolvedValue(fakeRes)
    await alertApi.acknowledge('a-001', 2)

    expect(mockPut).toHaveBeenCalledWith('/health/alerts/a-001/acknowledge', { version: 2 })
  })

  it('dismiss 应调用 PUT /health/alerts/:id/dismiss', async () => {
    mockPut.mockResolvedValue(fakeRes)
    await alertApi.dismiss('a-001', 1)

    expect(mockPut).toHaveBeenCalledWith('/health/alerts/a-001/dismiss', { version: 1 })
  })

  it('resolve 应调用 PUT /health/alerts/:id/resolve', async () => {
    mockPut.mockResolvedValue(fakeRes)
    await alertApi.resolve('a-001', 3)

    expect(mockPut).toHaveBeenCalledWith('/health/alerts/a-001/resolve', { version: 3 })
  })
})

describe('alertRuleApi', () => {
  const fakeRes = { data: { data: {} } }

  it('list 应调用 GET /health/alert-rules 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await alertRuleApi.list({ device_type: 'blood_pressure', page: 1, page_size: 10 })

    expect(mockGet).toHaveBeenCalledWith('/health/alert-rules', {
      params: { device_type: 'blood_pressure', page: 1, page_size: 10 },
    })
  })

  it('create 应调用 POST /health/alert-rules 并传递请求体', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = {
      name: '血压偏高告警',
      device_type: 'blood_pressure',
      condition_type: 'threshold',
      condition_params: { field: 'systolic', operator: '>', value: 140 },
      severity: 'high',
    }
    await alertRuleApi.create(req)

    expect(mockPost).toHaveBeenCalledWith('/health/alert-rules', req)
  })

  it('update 应调用 PUT /health/alert-rules/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { severity: 'critical', version: 1 }
    await alertRuleApi.update('rule-001', req)

    expect(mockPut).toHaveBeenCalledWith('/health/alert-rules/rule-001', req)
  })

  it('deactivate 应调用 PUT /health/alert-rules/:id/deactivate', async () => {
    mockPut.mockResolvedValue(fakeRes)
    await alertRuleApi.deactivate('rule-001', 2)

    expect(mockPut).toHaveBeenCalledWith('/health/alert-rules/rule-001/deactivate', { version: 2 })
  })
})
