/**
 * dictionaries 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 * as dictApi from './dictionaries'

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

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

  it('listDictionaries 应调用 GET /config/dictionaries 并传递分页参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await dictApi.listDictionaries(1, 10)

    expect(mockGet).toHaveBeenCalledWith('/config/dictionaries', {
      params: { page: 1, page_size: 10 },
    })
  })

  it('createDictionary 应调用 POST /config/dictionaries', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { name: '性别', code: 'gender', description: '性别字典' }
    await dictApi.createDictionary(req)

    expect(mockPost).toHaveBeenCalledWith('/config/dictionaries', req)
  })

  it('updateDictionary 应调用 PUT /config/dictionaries/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { name: '性别(更新)', version: 1 }
    await dictApi.updateDictionary('dict-001', req)

    expect(mockPut).toHaveBeenCalledWith('/config/dictionaries/dict-001', req)
  })

  it('deleteDictionary 应调用 DELETE /config/dictionaries/:id 并在 body 传递 version', async () => {
    mockDelete.mockResolvedValue(undefined)
    await dictApi.deleteDictionary('dict-001', 2)

    expect(mockDelete).toHaveBeenCalledWith('/config/dictionaries/dict-001', {
      data: { version: 2 },
    })
  })

  it('listItemsByCode 应调用 GET /config/dictionaries/items 并传递 code 参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await dictApi.listItemsByCode('gender')

    expect(mockGet).toHaveBeenCalledWith('/config/dictionaries/items', {
      params: { code: 'gender' },
    })
  })

  it('createDictionaryItem 应调用 POST /config/dictionaries/:id/items', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { label: '男', value: 'male', sort_order: 1 }
    await dictApi.createDictionaryItem('dict-001', req)

    expect(mockPost).toHaveBeenCalledWith('/config/dictionaries/dict-001/items', req)
  })

  it('updateDictionaryItem 应调用 PUT /config/dictionaries/:dictId/items/:itemId', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { label: '女', version: 1 }
    await dictApi.updateDictionaryItem('dict-001', 'item-001', req)

    expect(mockPut).toHaveBeenCalledWith('/config/dictionaries/dict-001/items/item-001', req)
  })

  it('deleteDictionaryItem 应调用 DELETE /config/dictionaries/:dictId/items/:itemId', async () => {
    mockDelete.mockResolvedValue(undefined)
    await dictApi.deleteDictionaryItem('dict-001', 'item-001', 1)

    expect(mockDelete).toHaveBeenCalledWith('/config/dictionaries/dict-001/items/item-001', {
      data: { version: 1 },
    })
  })
})
