import request from '@/utils/request';
import type { Page } from '@/types/base';
import type {
  FormDefinitionQuery,
  FormDefinitionResponse,
  FormDefinitionRequest,
} from './types';

const BASE_URL = '/api/v1/forms/definitions';

/**
 * 分页查询表单定义列表
 */
export const getDefinitions = (params?: FormDefinitionQuery) =>
  request.get<Page<FormDefinitionResponse>>(`${BASE_URL}/page`, { params });

/**
 * 获取表单定义详情
 */
export const getDefinitionById = (id: number) =>
  request.get<FormDefinitionResponse>(`${BASE_URL}/${id}`);

/**
 * 根据 key 获取表单定义（最新版本）
 */
export const getDefinitionByKey = (key: string) =>
  request.get<FormDefinitionResponse>(`${BASE_URL}/by-key/${key}`);

/**
 * 创建表单定义
 */
export const createDefinition = (data: FormDefinitionRequest) =>
  request.post<FormDefinitionResponse>(BASE_URL, data);

/**
 * 更新表单定义
 */
export const updateDefinition = (id: number, data: FormDefinitionRequest) =>
  request.put<FormDefinitionResponse>(`${BASE_URL}/${id}`, data);

/**
 * 发布表单
 */
export const publishDefinition = (id: number) =>
  request.post<FormDefinitionResponse>(`${BASE_URL}/${id}/publish`);

/**
 * 禁用表单
 */
export const disableDefinition = (id: number) =>
  request.post<FormDefinitionResponse>(`${BASE_URL}/${id}/disable`);

/**
 * 删除表单定义
 */
export const deleteDefinition = (id: number) =>
  request.delete(`${BASE_URL}/${id}`);

