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

export interface Dict {
  id: number;
  name: string;
  code: string;
  description?: string;
  validState?: number;
  creator?: number;
  modifier?: number;
  createTime?: string;
  modifyTime?: string;
  items?: DictItem[];
}

export interface DictItem {
  id: number;
  dictId: number;
  label: string;
  value: string;
  sort: number;
  validState: number;
}

export interface CreateDictForm {
  name: string;
  code: string;
  description?: string;
}

export interface UpdateDictForm {
  name?: string;
  code?: string;
  description?: string;
}

// 获取字典列表
export async function getDictsList(
  params: PaginationParams
): Promise<PaginationResponse<Dict>> {
  const response = await request.get<any, PaginationResponse<Dict>>("/dict", {
    params,
  });
  return response;
}

// 获取单个字典详情
export async function getDictDetail(id: number): Promise<Dict> {
  const response = await request.get<any, Dict>(`/dict/${id}`);
  return response;
}

// 根据编码获取字典
export async function getDictByCode(code: string): Promise<Dict> {
  const response = await request.get<any, Dict>(`/dict/code/${code}`);
  return response;
}

// 创建字典
export async function createDict(data: CreateDictForm): Promise<Dict> {
  const response = await request.post<any, Dict>("/dict", data);
  return response;
}

// 更新字典
export async function updateDict(
  id: number,
  data: UpdateDictForm
): Promise<Dict> {
  const response = await request.patch<any, Dict>(`/dict/${id}`, data);
  return response;
}

// 删除字典
export async function deleteDict(id: number): Promise<void> {
  return await request.delete<any, void>(`/dict/${id}`);
}

// 兼容性导出：保留 dictApi 对象
export const dictApi = {
  getList: getDictsList,
  getDetail: getDictDetail,
  getByCode: getDictByCode,
  create: createDict,
  update: updateDict,
  delete: deleteDict,
};

