/**
 * plugins 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 pluginsApi from './plugins'

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

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

  it('listPlugins 应调用 GET /admin/plugins 并传递分页和状态', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await pluginsApi.listPlugins(1, 10, 'enabled')

    expect(mockGet).toHaveBeenCalledWith('/admin/plugins', {
      params: { page: 1, page_size: 10, status: 'enabled' },
    })
  })

  it('getPlugin 应调用 GET /admin/plugins/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await pluginsApi.getPlugin('plug-001')

    expect(mockGet).toHaveBeenCalledWith('/admin/plugins/plug-001')
  })

  it('installPlugin 应调用 POST /admin/plugins/:id/install', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await pluginsApi.installPlugin('plug-001')

    expect(mockPost).toHaveBeenCalledWith('/admin/plugins/plug-001/install')
  })

  it('enablePlugin 应调用 POST /admin/plugins/:id/enable', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await pluginsApi.enablePlugin('plug-001')

    expect(mockPost).toHaveBeenCalledWith('/admin/plugins/plug-001/enable')
  })

  it('disablePlugin 应调用 POST /admin/plugins/:id/disable', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await pluginsApi.disablePlugin('plug-001')

    expect(mockPost).toHaveBeenCalledWith('/admin/plugins/plug-001/disable')
  })

  it('purgePlugin 应调用 DELETE /admin/plugins/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await pluginsApi.purgePlugin('plug-001')

    expect(mockDelete).toHaveBeenCalledWith('/admin/plugins/plug-001')
  })

  it('getPluginHealth 应调用 GET /admin/plugins/:id/health', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await pluginsApi.getPluginHealth('plug-001')

    expect(mockGet).toHaveBeenCalledWith('/admin/plugins/plug-001/health')
  })

  it('updatePluginConfig 应调用 PUT /admin/plugins/:id/config', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const config = { theme: 'dark' }
    await pluginsApi.updatePluginConfig('plug-001', config, 1)

    expect(mockPut).toHaveBeenCalledWith('/admin/plugins/plug-001/config', {
      config,
      version: 1,
    })
  })

  it('getPluginSchema 应调用 GET /admin/plugins/:id/schema', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await pluginsApi.getPluginSchema('plug-001')

    expect(mockGet).toHaveBeenCalledWith('/admin/plugins/plug-001/schema')
  })
})

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

  it('listMarketEntries 应调用 GET /market/entries', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await pluginsApi.listMarketEntries({ page: 1, page_size: 10, category: 'crm' })

    expect(mockGet).toHaveBeenCalledWith('/market/entries', {
      params: { page: 1, page_size: 10, category: 'crm' },
    })
  })

  it('getMarketEntry 应调用 GET /market/entries/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await pluginsApi.getMarketEntry('mkt-001')

    expect(mockGet).toHaveBeenCalledWith('/market/entries/mkt-001')
  })

  it('installFromMarket 应调用 POST /market/entries/:id/install', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await pluginsApi.installFromMarket('mkt-001')

    expect(mockPost).toHaveBeenCalledWith('/market/entries/mkt-001/install')
  })

  it('submitMarketReview 应调用 POST /market/entries/:id/reviews', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const review = { rating: 5, review_text: '很好用' }
    await pluginsApi.submitMarketReview('mkt-001', review)

    expect(mockPost).toHaveBeenCalledWith('/market/entries/mkt-001/reviews', review)
  })
})
