/**
 * workflow API 契约测试（definitions + instances + tasks）
 */
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 defApi from './workflowDefinitions'
import * as instApi from './workflowInstances'
import * as taskApi from './workflowTasks'

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

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

  it('listProcessDefinitions 应调用 GET /workflow/definitions', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await defApi.listProcessDefinitions(1, 10)

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

  it('getProcessDefinition 应调用 GET /workflow/definitions/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await defApi.getProcessDefinition('wf-001')

    expect(mockGet).toHaveBeenCalledWith('/workflow/definitions/wf-001')
  })

  it('createProcessDefinition 应调用 POST /workflow/definitions', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { name: '审批流程', key: 'approval', nodes: [], edges: [] }
    await defApi.createProcessDefinition(req)

    expect(mockPost).toHaveBeenCalledWith('/workflow/definitions', req)
  })

  it('publishProcessDefinition 应调用 POST /workflow/definitions/:id/publish', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await defApi.publishProcessDefinition('wf-001')

    expect(mockPost).toHaveBeenCalledWith('/workflow/definitions/wf-001/publish')
  })
})

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

  it('startInstance 应调用 POST /workflow/instances', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { definition_id: 'wf-001', business_key: 'BIZ-001' }
    await instApi.startInstance(req)

    expect(mockPost).toHaveBeenCalledWith('/workflow/instances', req)
  })

  it('listInstances 应调用 GET /workflow/instances 并传递分页', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await instApi.listInstances(1, 10)

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

  it('suspendInstance 应调用 POST /workflow/instances/:id/suspend', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await instApi.suspendInstance('inst-001')

    expect(mockPost).toHaveBeenCalledWith('/workflow/instances/inst-001/suspend')
  })

  it('resumeInstance 应调用 POST /workflow/instances/:id/resume', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await instApi.resumeInstance('inst-001')

    expect(mockPost).toHaveBeenCalledWith('/workflow/instances/inst-001/resume')
  })

  it('terminateInstance 应调用 POST /workflow/instances/:id/terminate', async () => {
    mockPost.mockResolvedValue(fakeRes)
    await instApi.terminateInstance('inst-001')

    expect(mockPost).toHaveBeenCalledWith('/workflow/instances/inst-001/terminate')
  })
})

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

  it('listPendingTasks 应调用 GET /workflow/tasks/pending', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await taskApi.listPendingTasks(1, 10)

    expect(mockGet).toHaveBeenCalledWith('/workflow/tasks/pending', {
      params: { page: 1, page_size: 10 },
    })
  })

  it('listCompletedTasks 应调用 GET /workflow/tasks/completed', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await taskApi.listCompletedTasks(1, 10)

    expect(mockGet).toHaveBeenCalledWith('/workflow/tasks/completed', {
      params: { page: 1, page_size: 10 },
    })
  })

  it('completeTask 应调用 POST /workflow/tasks/:id/complete', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { outcome: 'approved', form_data: { comment: '同意' } }
    await taskApi.completeTask('task-001', req)

    expect(mockPost).toHaveBeenCalledWith('/workflow/tasks/task-001/complete', req)
  })

  it('delegateTask 应调用 POST /workflow/tasks/:id/delegate', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { delegate_to: 'u-002' }
    await taskApi.delegateTask('task-001', req)

    expect(mockPost).toHaveBeenCalledWith('/workflow/tasks/task-001/delegate', req)
  })
})
