import { UserResponse, CreateUserDto, UpdateUserDto, Role } from '@goodgo/types';
import { apiClient } from '../../services/api/client';

/**
 * EN: Query parameters for users list endpoint
 * VI: Tham số query cho endpoint danh sách users
 */
export interface GetUsersParams {
  /** EN: Page number for pagination / VI: Số trang cho pagination */
  page?: number;
  /** EN: Number of items per page / VI: Số items mỗi trang */
  limit?: number;
  /** EN: Search query for filtering users / VI: Query tìm kiếm để lọc users */
  search?: string;
  /** EN: Filter by user role / VI: Lọc theo vai trò user */
  role?: Role;
  /** EN: Filter by active status / VI: Lọc theo trạng thái active */
  isActive?: boolean;
  /** EN: Sort field / VI: Trường sắp xếp */
  sortBy?: 'email' | 'createdAt' | 'updatedAt';
  /** EN: Sort direction / VI: Hướng sắp xếp */
  sortOrder?: 'asc' | 'desc';
}

/**
 * EN: Response structure for paginated users list
 * VI: Cấu trúc response cho danh sách users phân trang
 */
export interface GetUsersResponse {
  /** EN: Array of user objects / VI: Mảng các objects user */
  data: UserResponse[];
  /** EN: Pagination metadata / VI: Metadata phân trang */
  pagination: {
    /** EN: Current page number / VI: Số trang hiện tại */
    page: number;
    /** EN: Items per page / VI: Items mỗi trang */
    limit: number;
    /** EN: Total number of items / VI: Tổng số items */
    total: number;
    /** EN: Total number of pages / VI: Tổng số trang */
    totalPages: number;
  };
}

/**
 * EN: Fetch paginated list of users
 * VI: Lấy danh sách users phân trang
 *
 * @param params - Query parameters for filtering and pagination
 * @returns Promise resolving to paginated users response
 */
export async function getUsers(params: GetUsersParams = {}): Promise<GetUsersResponse> {
  const response = await apiClient.get('/users', { params });
  return response.data;
}

/**
 * EN: Fetch single user by ID
 * VI: Lấy thông tin user theo ID
 *
 * @param id - User ID
 * @returns Promise resolving to user response
 */
export async function getUser(id: string): Promise<UserResponse> {
  const response = await apiClient.get(`/users/${id}`);
  return response.data;
}

/**
 * EN: Create new user
 * VI: Tạo user mới
 *
 * @param payload - User creation data
 * @returns Promise resolving to created user response
 */
export async function createUser(payload: CreateUserDto): Promise<UserResponse> {
  const response = await apiClient.post('/users', payload);
  return response.data;
}

/**
 * EN: Update existing user
 * VI: Cập nhật user hiện có
 *
 * @param id - User ID
 * @param payload - User update data
 * @returns Promise resolving to updated user response
 */
export async function updateUser(id: string, payload: UpdateUserDto): Promise<UserResponse> {
  const response = await apiClient.put(`/users/${id}`, payload);
  return response.data;
}

/**
 * EN: Delete user by ID
 * VI: Xóa user theo ID
 *
 * @param id - User ID
 * @returns Promise resolving when deletion is complete
 */
export async function deleteUser(id: string): Promise<void> {
  await apiClient.delete(`/users/${id}`);
}

/**
 * EN: Bulk delete multiple users
 * VI: Xóa nhiều users cùng lúc
 *
 * @param ids - Array of user IDs to delete
 * @returns Promise resolving when bulk deletion is complete
 */
export async function bulkDeleteUsers(ids: string[]): Promise<void> {
  await apiClient.post('/users/bulk-delete', { ids });
}

/**
 * EN: Bulk update user roles
 * VI: Cập nhật vai trò cho nhiều users cùng lúc
 *
 * @param updates - Array of user ID and new role pairs
 * @returns Promise resolving when bulk update is complete
 */
export async function bulkUpdateUserRoles(updates: Array<{ id: string; role: Role }>): Promise<void> {
  await apiClient.post('/users/bulk-update-roles', { updates });
}