import { requestClient } from '#/api/request';

const prefix = 'file-group/';

export interface FileGroupRecord {
  id: number;
  name: string;
  sort: number;
  created_at?: number;
  updated_at?: number;
}

export interface FileGroupOptionItem {
  id: number;
  name: string;
  sort: number;
}

/** 分组选项缓存结构 */
export interface FileGroupCachePayload {
  hash: string;
  items: FileGroupOptionItem[];
}

/**
 * 管理端分组列表
 */
export async function getFileGroupList() {
  return requestClient.get<FileGroupRecord[]>(`${prefix}list`);
}

/**
 * 带 hash 的分组选项：无变化时 result 为 0
 */
export async function getFileGroupOptions(hash?: string) {
  return requestClient.get<FileGroupOptionItem[] | 0>(`${prefix}options`, {
    params: hash ? { hash } : undefined,
  });
}

export async function createFileGroup(data: Partial<FileGroupRecord>) {
  return requestClient.post<any>(`${prefix}create`, data);
}

export async function updateFileGroup(data: Partial<FileGroupRecord> & { id: number }) {
  return requestClient.post<any>(`${prefix}update`, data);
}

export async function deleteFileGroup(id: number) {
  return requestClient.post<any>(`${prefix}delete`, { id });
}
