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

export interface Judge {
  id: number;
  username: string;
  nickname: string;
  email?: string;
  phone?: string;
  gender?: 'male' | 'female';
  avatar?: string;
  status?: 'enabled' | 'disabled';
  organization?: string;
  validState?: number;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  roles?: Array<{
    id: number;
    role: {
      id: number;
      name: string;
      code: string;
    };
  }>;
  contestJudges?: Array<{
    contest: {
      id: number;
      contestName: string;
      status: string;
      contestState?: string;
    };
  }>;
}

export interface QueryJudgeParams extends PaginationParams {
  organization?: string;
  nickname?: string;
  username?: string;
  status?: 'enabled' | 'disabled';
}

export interface CreateJudgeForm {
  nickname: string;
  gender: 'male' | 'female';
  organization: string;
  phone: string;
  password: string;
  username?: string;
  email?: string;
  avatar?: string;
  status?: 'enabled' | 'disabled';
}

export interface UpdateJudgeForm {
  nickname?: string;
  gender?: 'male' | 'female';
  organization?: string;
  phone?: string;
  password?: string;
  username?: string;
  email?: string;
  avatar?: string;
  status?: 'enabled' | 'disabled';
}

export interface JudgeListResponse {
  list: Judge[];
  total: number;
  page: number;
  pageSize: number;
}

// 获取评委列表
export async function getJudgesList(
  params: QueryJudgeParams
): Promise<JudgeListResponse> {
  const response = await request.get<any, JudgeListResponse>(
    "/api/contests/judges",
    {
      params,
    }
  );
  return response;
}

// 获取单个评委详情
export async function getJudgeDetail(id: number): Promise<Judge> {
  const response = await request.get<any, Judge>(`/api/contests/judges/${id}`);
  return response;
}

// 创建评委
export async function createJudge(data: CreateJudgeForm): Promise<Judge> {
  const response = await request.post<any, Judge>("/api/contests/judges", data);
  return response;
}

// 更新评委
export async function updateJudge(
  id: number,
  data: UpdateJudgeForm
): Promise<Judge> {
  const response = await request.post<any, Judge>(
    `/api/contests/judges/${id}`,
    data
  );
  return response;
}

// 删除评委
export async function deleteJudge(id: number): Promise<void> {
  return await request.delete<any, void>(`/api/contests/judges/${id}`);
}

// 冻结评委
export async function freezeJudge(id: number): Promise<Judge> {
  const response = await request.post<any, Judge>(
    "/api/contests/judges/freeze",
    { id }
  );
  return response;
}

// 解冻评委
export async function unfreezeJudge(id: number): Promise<Judge> {
  const response = await request.post<any, Judge>(
    "/api/contests/judges/unfreeze",
    { id }
  );
  return response;
}

// 批量删除评委
export async function batchDeleteJudges(ids: number[]): Promise<void> {
  return await request.post<any, void>("/api/contests/judges/batch-delete", {
    ids,
  });
}

// 兼容性导出：保留 judgesManagementApi 对象
export const judgesManagementApi = {
  getList: getJudgesList,
  getDetail: getJudgeDetail,
  create: createJudge,
  update: updateJudge,
  delete: deleteJudge,
  freeze: freezeJudge,
  unfreeze: unfreezeJudge,
  batchDelete: batchDeleteJudges,
};






