import request from '@/utils/request';
import {
    WorkflowDefinition, 
    WorkflowDefinitionQuery,
    WorkflowCategoryResponse,
    WorkflowCategoryQuery,
    WorkflowCategoryRequest
} from './types';
import {Page} from '@/types/base';

const DEFINITION_URL = '/api/v1/workflow/definition';
const INSTANCE_URL = '/api/v1/workflow/instance';
const CATEGORY_URL = '/api/v1/workflow/categories';

export const getDefinitions = (params?: WorkflowDefinitionQuery) =>
    request.get<Page<WorkflowDefinition>>(`${DEFINITION_URL}/page`, {params});

export const getDefinitionDetail = (id: number) =>
    request.get<WorkflowDefinition>(`${DEFINITION_URL}/${id}`);

export const getPublishedDefinitions = () =>
    request.get<WorkflowDefinition[]>(`${DEFINITION_URL}/published`);

export const deployDefinition = (id: number) =>
    request.post<void>(`${DEFINITION_URL}/${id}/deploy`);

export const deleteDefinition = (id: number) =>
    request.delete<void>(`${DEFINITION_URL}/${id}`);

export const saveDefinition = (data: WorkflowDefinition) =>
    request.post<WorkflowDefinition>(`${DEFINITION_URL}`, data);

export const updateDefinition = (id: number, data: WorkflowDefinition) =>
    request.put<WorkflowDefinition>(`${DEFINITION_URL}/${id}`, data);

/**
 * 发布工作流定义
 * @param id 工作流定义ID
 * @returns Promise<void>
 */
export const publishDefinition = (id: number) =>
    request.post<void>(`${DEFINITION_URL}/${id}/published`);

/**
 * 分页查询工作流分类
 * @param params 查询参数
 * @returns Promise<Page<WorkflowCategoryResponse>>
 */
export const getWorkflowCategories = (params?: WorkflowCategoryQuery) =>
    request.get<Page<WorkflowCategoryResponse>>(`${CATEGORY_URL}/page`, {params});

/**
 * 查询工作流分类列表
 * @returns Promise<WorkflowCategoryResponse[]>
 */
export const getWorkflowCategoryList = () =>
    request.get<WorkflowCategoryResponse[]>(`${CATEGORY_URL}/list`);

/**
 * 获取单个工作流分类
 * @param id 分类ID
 * @returns Promise<WorkflowCategoryResponse>
 */
export const getWorkflowCategoryById = (id: number) =>
    request.get<WorkflowCategoryResponse>(`${CATEGORY_URL}/${id}`);

/**
 * 创建工作流分类
 * @param data 分类数据
 * @returns Promise<WorkflowCategoryResponse>
 */
export const createWorkflowCategory = (data: WorkflowCategoryRequest) =>
    request.post<WorkflowCategoryResponse>(CATEGORY_URL, data);

/**
 * 更新工作流分类
 * @param id 分类ID
 * @param data 分类数据
 * @returns Promise<WorkflowCategoryResponse>
 */
export const updateWorkflowCategory = (id: number, data: WorkflowCategoryRequest) =>
    request.put<WorkflowCategoryResponse>(`${CATEGORY_URL}/${id}`, data);

/**
 * 删除工作流分类
 * @param id 分类ID
 * @returns Promise<void>
 */
export const deleteWorkflowCategory = (id: number) =>
    request.delete<void>(`${CATEGORY_URL}/${id}`);

/**
 * 启动工作流实例
 * @param processKey 流程定义key
 * @param categoryId 分类ID
 * @returns Promise<void>
 */
export const startWorkflowInstance = (processKey: string, categoryId?: number) =>
    request.post<void>(`${INSTANCE_URL}/start`, {
        processKey,
        businessKey: `workflow_${Date.now()}`,
        categoryId
    });