import { request } from "@/api/request";

/** 分类类型 */
export interface Category {
  id: number;
  created_at: string;
  updated_at: string;
  name: string;
  description: string;
  type: string;
  icon: string;
  color: string;
  parent_id: number;
  level: number;
  path: string;
  sort_order: number;
  status: string;
  creator_id: number;
  creator_name: string;
  doc_count: number;
  faq_count: number;
  metadata: string | null;
  remarks: string;
}

/** API响应包装类型 */
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

/** 创建分类请求参数 */
export interface CreateCategoryParams {
  name: string;
  description?: string;
  type?: string;
  icon?: string;
  color?: string;
  parent_id?: number;
  sort_order?: number;
  remarks?: string;
}

/** 更新分类请求参数 */
export interface UpdateCategoryParams {
  id: number;
  name?: string;
  description?: string;
  icon?: string;
  color?: string;
  sort_order?: number;
  status?: string;
  remarks?: string;
}

/** 获取分类列表参数 */
export interface FetchCategoryListParams {
  type?: string;
  parent_id?: number;
}

/** 创建分类 */
export const createCategory = (data: CreateCategoryParams) => {
  return request.post<ApiResponse<Category>>("/Kb/v1/category/create", data);
};

/** 更新分类 */
export const updateCategory = (data: UpdateCategoryParams) => {
  return request.post<ApiResponse<Category>>("/Kb/v1/category/update", data);
};

/** 删除分类 */
export const deleteCategory = (id: number) => {
  return request.delete<ApiResponse<string>>(`/Kb/v1/category/${id}`);
};

/** 获取分类详情 */
export const fetchCategoryDetail = (id: number) => {
  return request.get<ApiResponse<Category>>(`/Kb/v1/category/${id}`);
};

/** 获取分类列表 */
export const fetchCategoryList = (params?: FetchCategoryListParams) => {
  return request.get<ApiResponse<Category[]>>("/Kb/v1/category/list", { params });
};

/** 获取分类树 */
export const fetchCategoryTree = (type?: string) => {
  return request.get<ApiResponse<Category[]>>("/Kb/v1/category/tree", {
    params: type ? { type } : undefined,
  });
};