import request from "@/utils/request";

export interface PresetComment {
  id: number;
  contestId: number;
  judgeId: number;
  content: string;
  score?: number;
  sortOrder: number;
  useCount: number;
  validState: number;
  creator?: number;
  modifier?: number;
  createTime: string;
  modifyTime: string;
}

export interface CreatePresetCommentParams {
  contestId: number;
  content: string;
  score?: number;
  sortOrder?: number;
}

export interface UpdatePresetCommentParams {
  content?: string;
  score?: number;
  sortOrder?: number;
}

export interface SyncPresetCommentsParams {
  sourceContestId: number;
  targetContestIds: number[];
}

export interface JudgeContest {
  id: number;
  contestName: string;
  contestState: string;
  status: string;
}

// 获取预设评语列表
export async function getPresetCommentsList(
  contestId: number
): Promise<PresetComment[]> {
  const response = await request.get<any, PresetComment[]>(
    "/contests/preset-comments",
    {
      params: { contestId },
    }
  );
  return response;
}

// 获取单个预设评语详情
export async function getPresetCommentDetail(
  id: number
): Promise<PresetComment> {
  const response = await request.get<any, PresetComment>(
    `/contests/preset-comments/${id}`
  );
  return response;
}

// 创建预设评语
export async function createPresetComment(
  data: CreatePresetCommentParams
): Promise<PresetComment> {
  const response = await request.post<any, PresetComment>(
    "/contests/preset-comments",
    data
  );
  return response;
}

// 更新预设评语
export async function updatePresetComment(
  id: number,
  data: UpdatePresetCommentParams
): Promise<PresetComment> {
  const response = await request.patch<any, PresetComment>(
    `/contests/preset-comments/${id}`,
    data
  );
  return response;
}

// 删除预设评语
export async function deletePresetComment(id: number): Promise<void> {
  return await request.delete<any, void>(`/contests/preset-comments/${id}`);
}

// 批量删除预设评语
export async function batchDeletePresetComments(ids: number[]): Promise<void> {
  return await request.post<any, void>("/contests/preset-comments/batch-delete", {
    ids,
  });
}

// 同步预设评语到其他活动
export async function syncPresetComments(
  data: SyncPresetCommentsParams
): Promise<{ message: string; count: number }> {
  const response = await request.post<any, { message: string; count: number }>(
    "/contests/preset-comments/sync",
    data
  );
  return response;
}

// 获取评委的活动列表
export async function getJudgeContests(): Promise<JudgeContest[]> {
  const response = await request.get<any, JudgeContest[]>(
    "/contests/preset-comments/judge/contests"
  );
  return response;
}

// 增加使用次数
export async function incrementUseCount(id: number): Promise<PresetComment> {
  const response = await request.post<any, PresetComment>(
    `/contests/preset-comments/${id}/use`
  );
  return response;
}

// 兼容性导出：保留 presetCommentsApi 对象
export const presetCommentsApi = {
  getList: getPresetCommentsList,
  getDetail: getPresetCommentDetail,
  create: createPresetComment,
  update: updatePresetComment,
  delete: deletePresetComment,
  batchDelete: batchDeletePresetComments,
  sync: syncPresetComments,
  getJudgeContests: getJudgeContests,
  incrementUseCount: incrementUseCount,
};
