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

export interface UserQueryParams {
  page?: number;
  pageSize?: number;
  keyword?: string;
  userType?: "platform" | "org" | "judge" | "public";
  filterTenantId?: number;
  userSource?: "admin_created" | "self_registered";
  status?: "enabled" | "disabled";
}

export interface UserTenant {
  id: number;
  name: string;
  code: string;
  tenantType: string;
  isSuper: number;
}

export interface User {
  id: number;
  username: string;
  nickname: string;
  email?: string;
  phone?: string;
  gender?: "male" | "female";
  avatar?: string;
  status?: "enabled" | "disabled";
  userSource?: string;
  city?: string;
  birthday?: string;
  organization?: string;
  tenantId?: number;
  validState?: number;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  tenant?: UserTenant;
  roles?: Array<{
    id: number;
    role: {
      id: number;
      name: string;
      code: string;
    };
  }>;
  _count?: {
    children: number;
    contestRegistrations: number;
  };
  // 详情接口返回
  children?: Array<{
    id: number;
    name: string;
    gender?: string;
    birthday?: string;
    grade?: string;
    city?: string;
    schoolName?: string;
  }>;
  contestRegistrations?: Array<{
    id: number;
    registrationState: string;
    participantType: string;
    createTime: string;
    contest: { id: number; contestName: string; contestState: string };
    child?: { id: number; name: string };
  }>;
  contestJudges?: Array<{
    contest: {
      id: number;
      contestName: string;
      status: string;
    };
  }>;
}

export interface UserStats {
  total: number;
  platform: number;
  org: number;
  judge: number;
  public: number;
}

export interface CreateUserForm {
  username: string;
  password: string;
  nickname: string;
  email?: string;
  phone?: string;
  gender?: "male" | "female";
  avatar?: string;
  status?: "enabled" | "disabled";
  roleIds?: number[];
}

export interface UpdateUserForm {
  username?: string;
  password?: string;
  nickname?: string;
  email?: string;
  phone?: string;
  gender?: "male" | "female";
  avatar?: string;
  status?: "enabled" | "disabled";
  roleIds?: number[];
}

// 获取用户列表
export async function getUsersList(
  params: UserQueryParams
): Promise<PaginationResponse<User>> {
  const response = await request.get<any, PaginationResponse<User>>("/api/users", {
    params,
  });
  return response;
}

// 获取用户统计
export async function getUserStats(): Promise<UserStats> {
  const response = await request.get<any, UserStats>("/api/users/stats");
  return response;
}

// 获取单个用户详情
export async function getUserDetail(id: number): Promise<User> {
  const response = await request.get<any, User>(`/api/users/${id}`);
  return response;
}

// 创建用户
export async function createUser(data: CreateUserForm): Promise<User> {
  const response = await request.post<any, User>("/api/users", data);
  return response;
}

// 更新用户
export async function updateUser(
  id: number,
  data: UpdateUserForm
): Promise<User> {
  const response = await request.put<any, User>(`/api/users/${id}`, data);
  return response;
}

// 切换用户状态
export async function updateUserStatus(
  id: number,
  status: "enabled" | "disabled"
): Promise<void> {
  await request.patch(`/api/users/${id}/status`, { status });
}

// 删除用户
export async function deleteUser(id: number): Promise<void> {
  return await request.delete<any, void>(`/api/users/${id}`);
}

// 兼容性导出：保留 usersApi 对象
export const usersApi = {
  getList: getUsersList,
  getStats: getUserStats,
  getDetail: getUserDetail,
  create: createUser,
  update: updateUser,
  updateStatus: updateUserStatus,
  delete: deleteUser,
};
