/**
 * 用户管理模块 API
 */
import request from '../request'
import { UserInfo } from '../auth'

// 用户查询参数
export interface UserQueryParams {
  page?: number
  size?: number
  keyword?: string
  role?: string
  status?: string
  teamId?: number
}

// 用户列表响应
export interface UserListResponse {
  items: UserInfo[]
  total: number
  page: number
  size: number
}

// 更新用户信息请求
export interface UpdateUserRequest {
  name?: string
  email?: string
  phone?: string
  avatar?: string
  role?: string
  status?: string
  teamId?: number
}

// 团队信息
export interface TeamInfo {
  id: number
  name: string
  description?: string
  leaderId: number
  leaderName: string
  memberCount: number
  status: 'active' | 'inactive'
  createdAt: string
  updatedAt: string
}

// 团队查询参数
export interface TeamQueryParams {
  page?: number
  size?: number
  keyword?: string
  status?: string
}

// 团队列表响应
export interface TeamListResponse {
  items: TeamInfo[]
  total: number
  page: number
  size: number
}

// 创建团队请求
export interface CreateTeamRequest {
  name: string
  description?: string
  leaderId?: number
}

// 团队成员
export interface TeamMember {
  id: number
  userId: number
  userName: string
  userEmail: string
  role: string
  joinedAt: string
}

/**
 * 获取用户列表
 */
export const getUserList = (params: UserQueryParams = {}) => {
  return request.get<UserListResponse>('/api/v1/users', { params })
}

/**
 * 获取用户详情
 */
export const getUserDetail = (id: number) => {
  return request.get<UserInfo>(`/api/v1/users/${id}`)
}

/**
 * 更新用户信息
 */
export const updateUser = (id: number, data: UpdateUserRequest) => {
  return request.put<UserInfo>(`/api/v1/users/${id}`, data)
}

/**
 * 删除用户
 */
export const deleteUser = (id: number) => {
  return request.delete(`/api/v1/users/${id}`)
}

/**
 * 获取当前用户信息
 */
export const getCurrentUserProfile = () => {
  return request.get<UserInfo>('/api/v1/users/me')
}

/**
 * 更新当前用户信息
 */
export const updateCurrentUserProfile = (data: UpdateUserRequest) => {
  return request.put<UserInfo>('/api/v1/users/me', data)
}

/**
 * 修改密码请求参数
 */
export interface ChangePasswordRequest {
  old_password: string
  new_password: string
}

/**
 * 修改当前用户密码
 */
export const changePassword = (data: ChangePasswordRequest) => {
  return request.put('/api/v1/users/me/password', data)
}

/**
 * 获取团队列表（用于下拉）
 */
export interface TeamBasic {
  id: number
  name: string
  code: string
  team_type: string
}

export const getTeams = (params?: { keyword?: string }) => {
  return request.get<TeamBasic[]>('/api/v1/teams', params)
}

/**
 * 将用户加入团队
 */
export const addUserToTeam = (userId: number, teamId: number, role: 'member'|'leader' = 'member') => {
  return request.post(`/api/v1/users/${userId}/teams/${teamId}?role=${role}`)
}

/**
 * 将用户从团队移除
 */
export const removeUserFromTeam = (userId: number, teamId: number) => {
  return request.delete(`/api/v1/users/${userId}/teams/${teamId}`)
}

/**
 * 获取用户所属岗位列表
 */
export interface UserPositionBasic {
  id: number
  name: string
  code: string
}

export const getUserPositions = (userId: number) => {
  return request.get<UserPositionBasic[]>(`/api/v1/users/${userId}/positions`)
}

/**
 * 获取当前用户学习统计
 */
export interface UserStatistics {
  learningDays: number
  totalHours: number
  practiceQuestions: number
  averageScore: number
}

export const getCurrentUserStatistics = () => {
  return request.get<UserStatistics>('/api/v1/users/me/statistics')
}

/**
 * 获取团队列表
 */
export const getTeamList = (params: TeamQueryParams = {}) => {
  return request.get<TeamListResponse>('/api/v1/teams', { params })
}

/**
 * 创建团队
 */
export const createTeam = (data: CreateTeamRequest) => {
  return request.post<TeamInfo>('/api/v1/teams', data)
}

/**
 * 获取团队详情
 */
export const getTeamDetail = (id: number) => {
  return request.get<TeamInfo>(`/api/v1/teams/${id}`)
}

/**
 * 更新团队信息
 */
export const updateTeam = (id: number, data: Partial<CreateTeamRequest>) => {
  return request.put<TeamInfo>(`/api/v1/teams/${id}`, data)
}

/**
 * 删除团队
 */
export const deleteTeam = (id: number) => {
  return request.delete(`/api/v1/teams/${id}`)
}

/**
 * 添加团队成员
 */
export const addTeamMember = (teamId: number, userIds: number[]) => {
  return request.post(`/api/v1/teams/${teamId}/members`, { userIds })
}

/**
 * 移除团队成员
 */
export const removeTeamMember = (teamId: number, userId: number) => {
  return request.delete(`/api/v1/teams/${teamId}/members/${userId}`)
}

/**
 * 获取团队成员列表
 */
export const getTeamMembers = (teamId: number) => {
  return request.get<TeamMember[]>(`/api/v1/teams/${teamId}/members`)
}
