import apiClient from '@/lib/api-client';

export interface User {
  id: string;
  username: string;
  displayName: string;
  email: string;
  emailVerified: boolean | null;
  phone: string | null;
  phoneVerified: boolean | null;
  status: string;
  department: {
    id: string;
    name: string;
  } | null;
  position: {
    id: string;
    title: string;
  } | null;
  roles: Array<{
    id: string;
    name: string;
    displayName: string;
  }>;
  createdAt: string;
  updatedAt: string;
}

// 符合 API 规范的列表响应结构（经过 apiClient 拦截器解包后）
export interface UsersResponse {
  items: User[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
  hasNext: boolean;
  hasPrev: boolean;
}

// 用户状态枚举（与后端保持一致）
export type UserStatus = 'ACTIVE' | 'INACTIVE' | 'SUSPENDED' | 'TERMINATED';

export interface GetUsersParams {
  page?: number;
  pageSize?: number;
  username?: string;
  displayName?: string;
  email?: string;
  departmentId?: string;
  status?: UserStatus;
  search?: string;
}

/**
 * 获取用户列表
 * GET /api/v1/users
 * 返回符合 API 规范的分页列表结构
 */
export async function getUsers(params?: GetUsersParams): Promise<UsersResponse> {
  // 将 search 参数映射为后端的 keyword 参数
  const apiParams = {
    ...params,
    keyword: params?.search,
    search: undefined, // 移除 search 参数
  };
  
  return apiClient.get('/users', { params: apiParams });
}

/**
 * 获取当前用户信息
 * GET /api/v1/users/me
 */
export async function getCurrentUser(): Promise<User> {
  return apiClient.get('/users/me');
}

/**
 * 根据 ID 获取用户
 * GET /api/v1/users/:id
 */
export async function getUserById(id: string): Promise<User> {
  return apiClient.get(`/users/${id}`);
}
