import { http } from '@/api/request'
import type { FlowDefinition } from '@/types/flow'
import type { PageQuery, PageResult } from '@/types/api'
import type { AxiosRequestConfig } from 'axios'

export function listDefinition(
  params?: PageQuery & Partial<FlowDefinition>,
  config?: AxiosRequestConfig
): Promise<PageResult<FlowDefinition>> {
  return http.get('/flow/definition/page', { params, ...config })
}

export function getDefinition(id: number): Promise<FlowDefinition> {
  return http.get(`/flow/definition/${id}`)
}

export function addDefinition(data: Partial<FlowDefinition>): Promise<void> {
  return http.post('/flow/definition', data)
}

export function updateDefinition(data: Partial<FlowDefinition>): Promise<number> {
  return http.put('/flow/definition', data)
}

export function deleteDefinition(id: number): Promise<void> {
  return http.delete(`/flow/definition/${id}`)
}

export function publishDefinition(id: number): Promise<void> {
  return http.post(`/flow/definition/${id}/publish`)
}

export function stopDefinition(id: number): Promise<void> {
  return http.post(`/flow/definition/${id}/stop`)
}

export function enableDefinition(id: number): Promise<void> {
  return http.post(`/flow/definition/${id}/enable`)
}

export function listEnabled(): Promise<FlowDefinition[]> {
  return http.get('/flow/definition/list-enabled')
}
