import request from "@/utils/request";
import type { PaginationParams, PaginationResponse } from "@/types/api";

export interface Config {
  id: number;
  key: string;
  value: string;
  description?: string;
  creator?: number;
  modifier?: number;
  createTime?: string;
  modifyTime?: string;
}

export interface CreateConfigForm {
  key: string;
  value: string;
  description?: string;
}

export interface UpdateConfigForm {
  key?: string;
  value?: string;
  description?: string;
}

// 获取配置列表
export async function getConfigsList(
  params: PaginationParams
): Promise<PaginationResponse<Config>> {
  const response = await request.get<any, PaginationResponse<Config>>(
    "/config",
    {
      params,
    }
  );
  return response;
}

// 获取单个配置详情
export async function getConfigDetail(id: number): Promise<Config> {
  const response = await request.get<any, Config>(`/config/${id}`);
  return response;
}

// 根据key获取配置
export async function getConfigByKey(key: string): Promise<Config> {
  const response = await request.get<any, Config>(`/config/key/${key}`);
  return response;
}

// 创建配置
export async function createConfig(data: CreateConfigForm): Promise<Config> {
  const response = await request.post<any, Config>("/config", data);
  return response;
}

// 更新配置
export async function updateConfig(
  id: number,
  data: UpdateConfigForm
): Promise<Config> {
  const response = await request.patch<any, Config>(`/config/${id}`, data);
  return response;
}

// 删除配置
export async function deleteConfig(id: number): Promise<void> {
  return await request.delete<any, void>(`/config/${id}`);
}

// 兼容性导出：保留 configApi 对象
export const configApi = {
  getList: getConfigsList,
  getDetail: getConfigDetail,
  getByKey: getConfigByKey,
  create: createConfig,
  update: updateConfig,
  delete: deleteConfig,
};

