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

// ==================== 活动相关类型 ====================
export interface Contest {
  id: number;
  contestName: string;
  contestType: "individual" | "team";
  contestState: "unpublished" | "published";
  status: "ongoing" | "finished"; // 活动进度状态
  startTime: string;
  endTime: string;
  address?: string;
  content?: string;
  contestTenants?: number[];
  coverUrl?: string;
  posterUrl?: string;
  contactName?: string;
  contactPhone?: string;
  contactQrcode?: string;
  organizers?: string;
  coOrganizers?: string;
  sponsors?: string;
  // 报名配置
  registerStartTime: string;
  registerEndTime: string;
  registerState?: string;
  requireAudit: boolean; // 是否需要审核
  allowedGrades?: number[]; // 允许报名的年级
  allowedClasses?: number[]; // 允许报名的班级
  teamMinMembers?: number; // 团队最少人数
  teamMaxMembers?: number; // 团队最多人数
  // 作品配置
  submitRule: "once" | "resubmit";
  submitStartTime: string;
  submitEndTime: string;
  workType?: "image" | "video" | "document" | "code" | "other"; // 作品类型
  workRequirement?: string; // 作品要求
  // 评审配置
  reviewRuleId?: number;
  reviewStartTime: string;
  reviewEndTime: string;
  // 成果配置
  resultState: "unpublished" | "published"; // 成果发布状态
  resultPublishTime?: string;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
  attachments?: ContestAttachment[];
  reviewRule?: ContestReviewRule;
  _count?: {
    registrations: number;
    works: number;
    teams: number;
    judges: number;
  };
}

export interface CreateContestForm {
  id?: number; // 编辑模式时使用
  contestName: string;
  contestType: "individual" | "team";
  startTime: string;
  endTime: string;
  address?: string;
  content?: string;
  contestTenants?: number[];
  coverUrl?: string;
  posterUrl?: string;
  contactName?: string;
  contactPhone?: string;
  contactQrcode?: string;
  organizers?: string;
  coOrganizers?: string;
  sponsors?: string;
  // 报名配置
  registerStartTime: string;
  registerEndTime: string;
  requireAudit?: boolean;
  allowedGrades?: number[];
  allowedClasses?: number[];
  teamMinMembers?: number;
  teamMaxMembers?: number;
  // 作品配置
  submitRule?: "once" | "resubmit";
  submitStartTime: string;
  submitEndTime: string;
  workType?: "image" | "video" | "document" | "code" | "other";
  workRequirement?: string;
  // 评审配置
  reviewStartTime: string;
  reviewEndTime: string;
  resultPublishTime?: string;
}

export interface UpdateContestForm extends Partial<CreateContestForm> {
  contestState?: "unpublished" | "published";
}

export interface QueryContestParams extends PaginationParams {
  contestName?: string;
  contestState?: "unpublished" | "published";
  status?: "ongoing" | "finished";
  contestType?: string;
  visibility?: string;
  stage?: "unpublished" | "registering" | "submitting" | "reviewing" | "finished";
  creatorTenantId?: number;
  role?: "student" | "teacher" | "judge";
}

export interface ContestStats {
  total: number;
  unpublished: number;
  registering: number;
  submitting: number;
  reviewing: number;
  finished: number;
}

// ==================== 附件相关类型 ====================
export interface ContestAttachment {
  id: number;
  contestId: number;
  fileName: string;
  fileUrl: string;
  format?: string;
  fileType?: string;
  size?: string;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
}

export interface CreateAttachmentForm {
  contestId: number;
  fileName: string;
  fileUrl: string;
  format?: string;
  fileType?: string;
  size?: string;
}

// ==================== 评审规则相关类型 ====================
export interface ReviewDimension {
  name: string;
  percentage: number;
  description?: string;
}

export interface ContestReviewRule {
  id: number;
  tenantId: number;
  ruleName: string;
  ruleDescription?: string;
  judgeCount: number;
  dimensions: ReviewDimension[];
  calculationRule: "average" | "remove_max_min" | "remove_min";
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
  contests?: Array<{
    id: number;
    contestName: string;
  }>;
}

export interface CreateReviewRuleForm {
  ruleName: string;
  ruleDescription?: string;
  judgeCount: number;
  dimensions: ReviewDimension[];
  calculationRule?: "average" | "remove_max_min" | "remove_min";
}

// ==================== 报名相关类型 ====================
export interface ContestRegistration {
  id: number;
  contestId: number;
  tenantId: number;
  registrationType?: "individual" | "team";
  teamId?: number;
  teamName?: string;
  userId: number;
  accountNo: string;
  accountName: string;
  role?: string;
  registrationState: "pending" | "passed" | "rejected" | "withdrawn";
  registrant?: number;
  registrationTime: string;
  reason?: string;
  operator?: number;
  operationDate?: string;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  contest?: Contest;
  user?: {
    id: number;
    username: string;
    nickname: string;
    gender?: string;
    phone?: string;
    student?: {
      id: number;
      gender?: number;
      phone?: string;
      class?: {
        id: number;
        name: string;
        grade?: {
          id: number;
          name: string;
        };
      };
    };
  };
  teachers?: Array<{
    id: number;
    userId: number;
    isDefault: boolean;
    user: {
      id: number;
      username: string;
      nickname: string;
    };
  }>;
  team?: ContestTeam;
  _count?: {
    works: number;
  };
}

export interface CreateRegistrationForm {
  contestId: number;
  registrationType: "individual" | "team";
  teamId?: number;
  userId: number;
}

export interface ReviewRegistrationForm {
  registrationState: "pending" | "passed" | "rejected" | "withdrawn";
  reason?: string;
}

export interface QueryRegistrationParams extends PaginationParams {
  contestId?: number;
  registrationState?: "pending" | "passed" | "rejected" | "withdrawn";
  registrationType?: string;
  userId?: number;
  participantType?: "self" | "child";
  keyword?: string;
}

export interface RegistrationStats {
  total: number;
  pending: number;
  passed: number;
  rejected: number;
}

// ==================== 团队相关类型 ====================
export interface ContestTeam {
  id: number;
  tenantId: number;
  contestId: number;
  teamName: string;
  leaderUserId: number;
  maxMembers?: number;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
  registrationState?: string; // 报名状态
  registrationId?: number; // 报名记录ID
  leader?: {
    id: number;
    username: string;
    nickname: string;
  };
  members?: ContestTeamMember[];
  contest?: Contest;
  _count?: {
    members: number;
    registrations: number;
  };
}

export interface ContestTeamMember {
  id: number;
  tenantId: number;
  teamId: number;
  userId: number;
  role: "leader" | "member" | "mentor";
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  user?: {
    id: number;
    username: string;
    nickname: string;
  };
}

export interface CreateTeamForm {
  contestId: number;
  teamName: string;
  leaderId: number;
  memberIds?: number[];
  teacherIds?: number[];
  maxMembers?: number;
}

export interface InviteMemberForm {
  userId: number;
  role?: "leader" | "member" | "mentor";
}

// ==================== 作品相关类型 ====================
export interface ContestWork {
  id: number;
  tenantId: number;
  contestId: number;
  registrationId: number;
  workNo?: string;
  title: string;
  description?: string;
  files?: string[];
  version: number;
  isLatest: boolean;
  status: "submitted" | "locked" | "reviewing" | "rejected" | "accepted";
  submitTime: string;
  submitterUserId?: number;
  submitterAccountNo?: string;
  submitSource: string;
  previewUrl?: string;
  previewUrls?: string[];
  aiModelMeta?: any;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
  contest?: Contest;
  registration?: ContestRegistration;
  attachments?: ContestWorkAttachment[];
  assignments?: Array<{
    id: number;
    judgeId: number;
    status: string;
    judge?: {
      id: number;
      username: string;
      nickname: string;
    };
  }>;
  _count?: {
    scores: number;
    assignments: number;
  };
  // 评审统计字段（由后端计算返回）
  reviewedCount?: number;
  totalJudgesCount?: number;
  averageScore?: number | null;
}

export interface ContestWorkAttachment {
  id: number;
  tenantId: number;
  contestId: number;
  workId: number;
  fileName: string;
  fileUrl: string;
  format?: string;
  fileType?: string;
  size?: string;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
}

export interface SubmitWorkAttachment {
  fileName: string;
  fileUrl: string;
  fileType?: string;
  size?: string;
}

export interface SubmitWorkForm {
  registrationId: number;
  title: string;
  description?: string;
  files?: string[];
  previewUrl?: string;
  previewUrls?: string[];
  aiModelMeta?: any;
  attachments?: SubmitWorkAttachment[];
}

export interface QueryWorkParams extends PaginationParams {
  contestId?: number;
  registrationId?: number;
  status?: "submitted" | "locked" | "reviewing" | "rejected" | "accepted";
  title?: string;
  workNo?: string;
  username?: string;
  keyword?: string;
}

export interface WorksStats {
  total: number;
  submitted: number;
  reviewing: number;
  reviewed: number;
}

// ==================== 评审相关类型 ====================
export interface ContestWorkJudgeAssignment {
  id: number;
  contestId: number;
  workId: number;
  judgeId: number;
  assignmentTime: string;
  status: "assigned" | "reviewing" | "completed";
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  work?: ContestWork;
  judge?: {
    id: number;
    username: string;
    nickname: string;
  };
  scores?: ContestWorkScore[];
}

export interface ContestWorkScore {
  id: number;
  tenantId: number;
  contestId: number;
  workId: number;
  assignmentId: number;
  judgeId: number;
  judgeName: string;
  dimensionScores: any;
  totalScore: number;
  comments?: string;
  scoreTime: string;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
  work?: ContestWork;
  judge?: {
    id: number;
    username: string;
    nickname: string;
  };
}

export interface AssignWorkForm {
  workId: number;
  judgeIds: number[];
}

export interface CreateScoreForm {
  workId: number;
  assignmentId: number;
  dimensionScores?: any;
  totalScore: number;
  comments?: string;
}

// ==================== 公告相关类型 ====================
export interface ContestNotice {
  id: number;
  contestId: number;
  title: string;
  content: string;
  noticeType: "system" | "manual" | "urgent";
  priority: number;
  publishTime?: string;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
  contest?: Contest;
}

export interface CreateNoticeForm {
  contestId: number;
  title: string;
  content: string;
  noticeType?: "system" | "manual" | "urgent";
  priority?: number;
}

// ==================== 评委相关类型 ====================
export interface ContestJudge {
  id: number;
  contestId: number;
  judgeId: number;
  specialty?: string;
  weight?: number;
  description?: string;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  validState?: number;
  contest?: Contest;
  judge?: {
    id: number;
    username: string;
    nickname: string;
    email?: string;
    phone?: string;
    gender?: 'male' | 'female';
    status?: 'enabled' | 'disabled';
    tenantId?: number;
    tenant?: {
      id: number;
      name: string;
    };
    contestJudges?: Array<{
      contest: {
        id: number;
        contestName: string;
        status: string;
      };
    }>;
  };
  _count?: {
    assignedContestWorks: number;
    scoredContestWorks: number;
  };
}

export interface CreateJudgeForm {
  contestId: number;
  judgeId: number;
  specialty?: string;
  weight?: number;
  description?: string;
}

// ==================== API 函数 ====================

// 活动管理
export const contestsApi = {
  // 获取活动统计
  getStats: async (): Promise<ContestStats> => {
    const response = await request.get<any, ContestStats>("/api/contests/stats");
    return response;
  },

  // 获取活动列表
  getList: async (
    params: QueryContestParams
  ): Promise<PaginationResponse<Contest>> => {
    const response = await request.get<any, PaginationResponse<Contest>>(
      "/api/contests",
      { params }
    );
    return response;
  },

  // 获取我参与的活动列表
  getMyContests: async (
    params: QueryContestParams
  ): Promise<PaginationResponse<Contest>> => {
    const response = await request.get<any, PaginationResponse<Contest>>(
      "/api/contests/my-contests",
      { params }
    );
    return response;
  },

  // 获取活动详情
  getDetail: async (id: number): Promise<Contest> => {
    const response = await request.get<any, Contest>(`/api/contests/${id}`);
    return response;
  },

  // 创建活动
  create: async (data: CreateContestForm): Promise<Contest> => {
    const response = await request.post<any, Contest>("/api/contests", data);
    return response;
  },

  // 更新活动
  update: async (id: number, data: UpdateContestForm): Promise<Contest> => {
    const response = await request.put<any, Contest>(`/api/contests/${id}`, data);
    return response;
  },

  // 发布/撤回活动
  publish: async (
    id: number,
    contestState: "unpublished" | "published"
  ): Promise<Contest> => {
    const response = await request.patch<any, Contest>(
      `/api/contests/${id}/publish`,
      { contestState }
    );
    return response;
  },

  // 删除活动
  delete: async (id: number): Promise<void> => {
    return await request.delete<any, void>(`/api/contests/${id}`);
  },

  // 标记活动完结
  finish: async (id: number): Promise<Contest> => {
    const response = await request.patch<any, Contest>(`/api/contests/${id}/finish`);
    return response;
  },

  // 重新开启已完结的活动
  reopen: async (id: number): Promise<Contest> => {
    const response = await request.patch<any, Contest>(`/api/contests/${id}/reopen`);
    return response;
  },
};

// 附件管理
export const attachmentsApi = {
  // 获取活动附件列表
  getList: async (contestId: number): Promise<ContestAttachment[]> => {
    const response = await request.get<any, ContestAttachment[]>(
      `/api/contests/attachments/contest/${contestId}`
    );
    return response;
  },

  // 创建附件
  create: async (data: CreateAttachmentForm): Promise<ContestAttachment> => {
    const response = await request.post<any, ContestAttachment>(
      "/api/contests/attachments",
      data
    );
    return response;
  },

  // 删除附件
  delete: async (id: number): Promise<void> => {
    return await request.delete<any, void>(`/api/contests/attachments/${id}`);
  },
};

// 评审规则
export interface ReviewRule {
  id: number;
  tenantId: number;
  ruleName: string;
  ruleDescription?: string;
  judgeCount: number;
  dimensions: ReviewDimension[];
  calculationRule: "average" | "remove_max_min" | "remove_min";
  validState: number;
  contests?: Array<{
    id: number;
    contestName: string;
  }>;
  createTime?: string;
  updateTime?: string;
}

// 用于选择器的简化评审规则
export interface ReviewRuleForSelect {
  id: number;
  ruleName: string;
  ruleDescription?: string;
  judgeCount: number;
  calculationRule: string;
}

export interface QueryReviewRuleParams {
  ruleName?: string;
  page?: number;
  pageSize?: number;
}

export const reviewRulesApi = {
  // 获取评审规则列表
  getList: async (params?: QueryReviewRuleParams): Promise<{
    list: ReviewRule[];
    total: number;
    page: number;
    pageSize: number;
  }> => {
    const response = await request.get<any, {
      list: ReviewRule[];
      total: number;
      page: number;
      pageSize: number;
    }>("/api/contests/review-rules", { params });
    return response;
  },

  // 获取所有可用的评审规则（用于活动创建时选择）
  getForSelect: async (): Promise<ReviewRuleForSelect[]> => {
    const response = await request.get<any, ReviewRuleForSelect[]>(
      "/api/contests/review-rules/select"
    );
    return response;
  },

  // 获取评审规则详情
  getDetail: async (id: number): Promise<ReviewRule> => {
    const response = await request.get<any, ReviewRule>(
      `/api/contests/review-rules/${id}`
    );
    return response;
  },

  // 创建评审规则
  create: async (data: CreateReviewRuleForm): Promise<ReviewRule> => {
    const response = await request.post<any, ReviewRule>(
      "/api/contests/review-rules",
      data
    );
    return response;
  },

  // 更新评审规则
  update: async (
    id: number,
    data: Partial<CreateReviewRuleForm>
  ): Promise<ReviewRule> => {
    const response = await request.put<any, ReviewRule>(
      `/api/contests/review-rules/${id}`,
      data
    );
    return response;
  },

  // 删除评审规则
  delete: async (id: number): Promise<void> => {
    await request.delete(`/api/contests/review-rules/${id}`);
  },
};

// 报名管理
export const registrationsApi = {
  // 报名统计
  getStats: async (contestId?: number): Promise<RegistrationStats> => {
    const params: any = {};
    if (contestId) params.contestId = contestId;
    const response = await request.get<any, RegistrationStats>(
      "/api/contests/registrations/stats",
      { params }
    );
    return response;
  },

  // 获取报名列表
  getList: async (
    params: QueryRegistrationParams
  ): Promise<PaginationResponse<ContestRegistration>> => {
    const response = await request.get<
      any,
      PaginationResponse<ContestRegistration>
    >("/api/contests/registrations", { params });
    return response;
  },

  // 获取报名详情
  getDetail: async (id: number): Promise<ContestRegistration> => {
    const response = await request.get<any, ContestRegistration>(
      `/api/contests/registrations/${id}`
    );
    return response;
  },

  // 获取当前用户在某活动中的报名记录（包括作为团队成员的情况）
  getMyRegistration: async (
    contestId: number
  ): Promise<ContestRegistration | null> => {
    const response = await request.get<any, ContestRegistration | null>(
      `/api/contests/registrations/my/${contestId}`
    );
    return response;
  },

  // 创建报名
  create: async (
    data: CreateRegistrationForm
  ): Promise<ContestRegistration> => {
    const response = await request.post<any, ContestRegistration>(
      "/api/contests/registrations",
      data
    );
    return response;
  },

  // 添加指导老师
  addTeacher: async (
    registrationId: number,
    teacherUserId: number
  ): Promise<any> => {
    const response = await request.post<any, any>(
      `/api/contests/registrations/${registrationId}/teachers`,
      { teacherUserId }
    );
    return response;
  },

  // 移除指导老师
  removeTeacher: async (
    registrationId: number,
    teacherUserId: number
  ): Promise<void> => {
    await request.delete(
      `/api/contests/registrations/${registrationId}/teachers/${teacherUserId}`
    );
  },

  // 审核报名
  review: async (
    id: number,
    data: ReviewRegistrationForm
  ): Promise<ContestRegistration> => {
    const response = await request.patch<any, ContestRegistration>(
      `/api/contests/registrations/${id}/review`,
      data
    );
    return response;
  },

  // 删除报名
  delete: async (id: number): Promise<void> => {
    return await request.delete<any, void>(`/api/contests/registrations/${id}`);
  },
};

// 团队管理
export const teamsApi = {
  // 获取团队列表
  getList: async (contestId: number): Promise<ContestTeam[]> => {
    const response = await request.get<any, ContestTeam[]>(
      `/api/contests/teams/contest/${contestId}`
    );
    return response;
  },

  // 获取团队详情
  getDetail: async (id: number): Promise<ContestTeam> => {
    const response = await request.get<any, ContestTeam>(
      `/api/contests/teams/${id}`
    );
    return response;
  },

  // 创建团队
  create: async (data: CreateTeamForm): Promise<ContestTeam> => {
    const response = await request.post<any, ContestTeam>(
      "/api/contests/teams",
      data
    );
    return response;
  },

  // 更新团队
  update: async (
    id: number,
    data: Partial<CreateTeamForm>
  ): Promise<ContestTeam> => {
    const response = await request.put<any, ContestTeam>(
      `/api/contests/teams/${id}`,
      data
    );
    return response;
  },

  // 邀请成员
  inviteMember: async (
    teamId: number,
    data: InviteMemberForm
  ): Promise<ContestTeamMember> => {
    const response = await request.post<any, ContestTeamMember>(
      `/api/contests/teams/${teamId}/members`,
      data
    );
    return response;
  },

  // 移除成员
  removeMember: async (teamId: number, userId: number): Promise<void> => {
    return await request.delete<any, void>(
      `/api/contests/teams/${teamId}/members/${userId}`
    );
  },

  // 删除团队
  delete: async (id: number): Promise<void> => {
    return await request.delete<any, void>(`/api/contests/teams/${id}`);
  },
};

// 教师指导作品查询参数
export interface QueryGuidedWorkParams extends PaginationParams {
  contestId?: number;
  workNo?: string;
  playerName?: string;
  accountNo?: string;
}

// 教师指导的作品（含评审进度）
export interface GuidedWork extends ContestWork {
  reviewProgress: string;
}

// 作品管理
export const worksApi = {
  // 作品统计
  getStats: async (contestId?: number): Promise<WorksStats> => {
    const params: any = {};
    if (contestId) params.contestId = contestId;
    const response = await request.get<any, WorksStats>(
      "/api/contests/works/stats",
      { params }
    );
    return response;
  },

  // 获取作品列表
  getList: async (
    params: QueryWorkParams
  ): Promise<PaginationResponse<ContestWork>> => {
    const response = await request.get<any, PaginationResponse<ContestWork>>(
      "/api/contests/works",
      { params }
    );
    return response;
  },

  // 获取教师指导的作品列表
  getGuidedWorks: async (
    params: QueryGuidedWorkParams
  ): Promise<PaginationResponse<GuidedWork>> => {
    const response = await request.get<any, PaginationResponse<GuidedWork>>(
      "/api/contests/works/guided",
      { params }
    );
    return response;
  },

  // 获取作品详情
  getDetail: async (id: number): Promise<ContestWork> => {
    const response = await request.get<any, ContestWork>(
      `/api/contests/works/${id}`
    );
    return response;
  },

  // 提交作品
  submit: async (data: SubmitWorkForm): Promise<ContestWork> => {
    const response = await request.post<any, ContestWork>(
      "/api/contests/works/submit",
      data
    );
    return response;
  },

  // 获取作品版本列表
  getVersions: async (registrationId: number): Promise<ContestWork[]> => {
    const response = await request.get<any, ContestWork[]>(
      `/api/contests/works/registration/${registrationId}/versions`
    );
    return response;
  },

  // 删除作品
  delete: async (id: number): Promise<void> => {
    return await request.delete<any, void>(`/api/contests/works/${id}`);
  },
};

// ==================== 评审进度相关类型 ====================
export interface ReviewProgress {
  contest: {
    id: number;
    contestName: string;
    reviewStartTime: string;
    reviewEndTime: string;
    reviewRule: ContestReviewRule | null;
  };
  summary: {
    totalWorks: number;
    assignedWorksCount: number;
    scoredWorksCount: number;
    unassignedWorksCount: number;
    totalJudges: number;
    totalAssignments: number;
    totalScores: number;
    pendingScoresCount: number;
  };
  progress: {
    assignmentProgress: number;
    scoringProgress: number;
    overallProgress: number;
  };
  judgeProgress: JudgeProgressItem[];
  unassignedWorks: UnassignedWork[];
  pendingAssignments: PendingAssignment[];
}

export interface JudgeProgressItem {
  judgeId: number;
  judgeName: string;
  specialty: string | null;
  weight: number | null;
  assignedCount: number;
  scoredCount: number;
  pendingCount: number;
  progress: number;
}

export interface UnassignedWork {
  id: number;
  workNo: string | null;
  title: string;
  createTime: string;
  registration: {
    user: { id: number; nickname: string; username: string } | null;
    team: { id: number; teamName: string } | null;
  } | null;
}

export interface PendingAssignment {
  id: number;
  workId: number;
  workNo: string | null;
  workTitle: string;
  judgeId: number;
  judgeName: string;
  status: string;
  assignmentTime: string;
}

export interface WorkStatusStats {
  submitted: number;
  reviewing: number;
  reviewed: number;
  awarded: number;
  total: number;
}

export interface BatchAssignForm {
  workIds: number[];
  judgeIds: number[];
}

export interface BatchAssignResult {
  created: number;
  skipped: number;
  assignments: ContestWorkJudgeAssignment[];
}

export interface AutoAssignResult {
  message: string;
  worksCount?: number;
  created: number;
  judgesPerWork?: number;
}

// 评审管理
export const reviewsApi = {
  // 分配作品给评委
  assignWork: async (
    contestId: number,
    data: AssignWorkForm
  ): Promise<ContestWorkJudgeAssignment[]> => {
    const response = await request.post<any, ContestWorkJudgeAssignment[]>(
      `/api/contests/reviews/assign?contestId=${contestId}`,
      data
    );
    return response;
  },

  // 批量分配作品给评委
  batchAssignWorks: async (
    contestId: number,
    data: BatchAssignForm
  ): Promise<BatchAssignResult> => {
    const response = await request.post<any, BatchAssignResult>(
      `/api/contests/reviews/batch-assign?contestId=${contestId}`,
      data
    );
    return response;
  },

  // 自动分配作品给评委
  autoAssignWorks: async (contestId: number): Promise<AutoAssignResult> => {
    const response = await request.post<any, AutoAssignResult>(
      `/api/contests/reviews/auto-assign?contestId=${contestId}`
    );
    return response;
  },

  // 评分
  score: async (data: CreateScoreForm): Promise<ContestWorkScore> => {
    const response = await request.post<any, ContestWorkScore>(
      "/api/contests/reviews/score",
      data
    );
    return response;
  },

  // 更新评分
  updateScore: async (
    scoreId: number,
    data: Partial<CreateScoreForm>
  ): Promise<ContestWorkScore> => {
    const response = await request.put<any, ContestWorkScore>(
      `/api/contests/reviews/score/${scoreId}`,
      data
    );
    return response;
  },

  // 获取分配给当前评委的作品
  getAssignedWorks: async (
    contestId: number
  ): Promise<ContestWorkJudgeAssignment[]> => {
    const response = await request.get<any, ContestWorkJudgeAssignment[]>(
      `/api/contests/reviews/assigned?contestId=${contestId}`
    );
    return response;
  },

  // 获取评审进度统计
  getReviewProgress: async (contestId: number): Promise<ReviewProgress> => {
    const response = await request.get<any, ReviewProgress>(
      `/api/contests/reviews/progress/${contestId}`
    );
    return response;
  },

  // 获取作品状态统计
  getWorkStatusStats: async (contestId: number): Promise<WorkStatusStats> => {
    const response = await request.get<any, WorkStatusStats>(
      `/api/contests/reviews/work-status/${contestId}`
    );
    return response;
  },

  // 获取作品评分列表
  getWorkScores: async (workId: number): Promise<ContestWorkScore[]> => {
    const response = await request.get<any, ContestWorkScore[]>(
      `/api/contests/reviews/work/${workId}/scores`
    );
    return response;
  },

  // 计算最终得分
  calculateFinalScore: async (
    workId: number
  ): Promise<{
    finalScore: number;
    scoreCount: number;
    calculationRule: string;
  }> => {
    const response = await request.get<
      any,
      { finalScore: number; scoreCount: number; calculationRule: string }
    >(`/api/contests/reviews/work/${workId}/final-score`);
    return response;
  },

  // 替换评委
  replaceJudge: async (
    assignmentId: number,
    newJudgeId: number
  ): Promise<void> => {
    await request.post<any, void>(
      `/api/contests/reviews/replace-judge`,
      { assignmentId, newJudgeId }
    );
  },

  // 获取评委参与的活动列表
  getJudgeContests: async (): Promise<any[]> => {
    const response = await request.get<any, any[]>(
      `/api/contests/reviews/judge/contests`
    );
    return response;
  },

  // 获取评委在某个活动下分配的作品列表
  getJudgeContestWorks: async (
    contestId: number,
    params: {
      page?: number;
      pageSize?: number;
      workNo?: string;
      accountNo?: string;
      reviewStatus?: string;
    }
  ): Promise<{ list: any[]; total: number; page: number; pageSize: number }> => {
    const response = await request.get<
      any,
      { list: any[]; total: number; page: number; pageSize: number }
    >(`/api/contests/reviews/judge/contests/${contestId}/works`, { params });
    return response;
  },
};

// 公告管理
export const noticesApi = {
  // 获取公告列表（按活动）
  getList: async (contestId: number): Promise<ContestNotice[]> => {
    const response = await request.get<any, ContestNotice[]>(
      `/api/contests/notices/contest/${contestId}`
    );
    return response;
  },

  // 获取所有公告（支持搜索和分页）
  getAll: async (params?: {
    title?: string;
    publishDate?: string;
    page?: number;
    pageSize?: number;
  }): Promise<PaginationResponse<ContestNotice>> => {
    const response = await request.get<any, PaginationResponse<ContestNotice>>(
      '/api/contests/notices',
      { params }
    );
    return response;
  },

  // 获取公告详情
  getDetail: async (id: number): Promise<ContestNotice> => {
    const response = await request.get<any, ContestNotice>(
      `/api/contests/notices/${id}`
    );
    return response;
  },

  // 创建公告
  create: async (data: CreateNoticeForm): Promise<ContestNotice> => {
    const response = await request.post<any, ContestNotice>(
      "/api/contests/notices",
      data
    );
    return response;
  },

  // 更新公告
  update: async (
    id: number,
    data: Partial<CreateNoticeForm>
  ): Promise<ContestNotice> => {
    const response = await request.put<any, ContestNotice>(
      `/api/contests/notices/${id}`,
      data
    );
    return response;
  },

  // 删除公告
  delete: async (id: number): Promise<void> => {
    return await request.delete<any, void>(`/api/contests/notices/${id}`);
  },

  // 发布公告
  publish: async (id: number): Promise<ContestNotice> => {
    const response = await request.post<any, ContestNotice>(
      `/api/contests/notices/${id}/publish`
    );
    return response;
  },
};

// ==================== 成果相关类型 ====================
export interface ContestResult {
  id: number;
  workNo: string | null;
  title: string;
  finalScore: number | null;
  rank: number | null;
  awardLevel: string | null;
  awardName: string | null;
  certificateUrl: string | null;
  registration: {
    user: { id: number; username: string; nickname: string } | null;
    team: { id: number; teamName: string } | null;
  } | null;
}

export interface ResultsResponse {
  contest: {
    id: number;
    contestName: string;
    resultState: string;
    resultPublishTime: string | null;
    reviewRule: ContestReviewRule | null;
  };
  list: ContestResult[];
  total: number;
  page: number;
  pageSize: number;
  awardDistribution: Record<string, number>;
}

export interface ResultsSummary {
  contest: {
    id: number;
    contestName: string;
    resultState: string;
    resultPublishTime: string | null;
  };
  summary: {
    totalWorks: number;
    scoredWorks: number;
    rankedWorks: number;
    awardedWorks: number;
    unscoredWorks: number;
  };
  awardDistribution: Record<string, number>;
  scoreStats: {
    avgScore: string | null;
    maxScore: string | null;
    minScore: string | null;
  };
}

export interface SetAwardForm {
  awardLevel: 'first' | 'second' | 'third' | 'excellent' | 'none';
  awardName?: string;
  certificateUrl?: string;
}

export interface BatchSetAwardsForm {
  awards: Array<{
    workId: number;
    awardLevel: 'first' | 'second' | 'third' | 'excellent' | 'none';
    awardName?: string;
  }>;
}

export interface AutoSetAwardsForm {
  first?: number;
  second?: number;
  third?: number;
  excellent?: number;
}

// 成果管理
export const resultsApi = {
  // 计算所有作品的最终得分
  calculateScores: async (contestId: number): Promise<{ message: string; calculatedCount: number; calculationRule: string }> => {
    const response = await request.post<any, any>(
      `/api/contests/results/${contestId}/calculate-scores`
    );
    return response;
  },

  // 计算排名
  calculateRankings: async (contestId: number): Promise<{ message: string; rankedCount: number }> => {
    const response = await request.post<any, any>(
      `/api/contests/results/${contestId}/calculate-rankings`
    );
    return response;
  },

  // 设置单个作品奖项
  setAward: async (workId: number, data: SetAwardForm): Promise<ContestResult> => {
    const response = await request.patch<any, ContestResult>(
      `/api/contests/results/work/${workId}/award`,
      data
    );
    return response;
  },

  // 批量设置奖项
  batchSetAwards: async (contestId: number, data: BatchSetAwardsForm): Promise<any> => {
    const response = await request.post<any, any>(
      `/api/contests/results/${contestId}/batch-set-awards`,
      data
    );
    return response;
  },

  // 根据排名自动设置奖项
  autoSetAwards: async (contestId: number, data: AutoSetAwardsForm): Promise<any> => {
    const response = await request.post<any, any>(
      `/api/contests/results/${contestId}/auto-set-awards`,
      data
    );
    return response;
  },

  // 发布成果
  publish: async (contestId: number): Promise<any> => {
    const response = await request.post<any, any>(
      `/api/contests/results/${contestId}/publish`
    );
    return response;
  },

  // 撤回发布
  unpublish: async (contestId: number): Promise<any> => {
    const response = await request.post<any, any>(
      `/api/contests/results/${contestId}/unpublish`
    );
    return response;
  },

  // 获取活动结果列表（作品列表）
  getResults: async (
    contestId: number,
    params: {
      page?: number;
      pageSize?: number;
      workNo?: string;
      accountNo?: string;
    } = {}
  ): Promise<ResultsResponse> => {
    const response = await request.get<any, ResultsResponse>(
      `/api/contests/results/${contestId}`,
      { params: { page: params.page || 1, pageSize: params.pageSize || 10, ...params } }
    );
    return response;
  },

  // 获取活动结果统计摘要
  getSummary: async (contestId: number): Promise<ResultsSummary> => {
    const response = await request.get<any, ResultsSummary>(
      `/api/contests/results/${contestId}/summary`
    );
    return response;
  },
};

// 评委管理
export const judgesApi = {
  // 获取评委列表
  getList: async (contestId: number): Promise<ContestJudge[]> => {
    const response = await request.get<any, ContestJudge[]>(
      `/api/contests/judges/contest/${contestId}`
    );
    return response;
  },

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

  // 添加评委
  create: async (data: CreateJudgeForm): Promise<ContestJudge> => {
    const response = await request.post<any, ContestJudge>(
      "/api/contests/judges",
      data
    );
    return response;
  },

  // 更新评委
  update: async (
    id: number,
    data: Partial<CreateJudgeForm>
  ): Promise<ContestJudge> => {
    const response = await request.put<any, ContestJudge>(
      `/api/contests/judges/${id}`,
      data
    );
    return response;
  },

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