import api from '../../../shared/utils/api';

// ==================== DTOs ====================

export type UserType = 'ANONYMOUS' | 'REGISTERED';

export interface UserDto {
  id: string;
  type: UserType;
  phone: string | null;
  nickname: string | null;
  avatar: string | null;
  fingerprint?: string | null;
  createdAt: string;
  updatedAt?: string;
  lastActiveAt: string;
}

export interface PaginatedUsers {
  items: UserDto[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export interface UserStatistics {
  total: number;
  anonymous: number;
  registered: number;
  registrationRate: string;
}

export interface UserQueryParams {
  type?: UserType;
  phone?: string;
  nickname?: string;
  page?: number;
  pageSize?: number;
  sortBy?: 'createdAt' | 'lastActiveAt';
  sortOrder?: 'ASC' | 'DESC';
}

// ==================== API ====================

export const usersApi = {
  // List users with pagination
  listUsers: async (params: UserQueryParams): Promise<PaginatedUsers> => {
    const response = await api.get('/users/admin/list', { params });
    return response.data.data;
  },

  // Get user statistics
  getStatistics: async (): Promise<UserStatistics> => {
    const response = await api.get('/users/admin/statistics');
    return response.data.data;
  },

  // Search users
  searchUsers: async (keyword: string, limit = 10): Promise<UserDto[]> => {
    const response = await api.get('/users/admin/search', {
      params: { keyword, limit },
    });
    return response.data.data;
  },

  // Get user detail
  getUserDetail: async (id: string): Promise<UserDto> => {
    const response = await api.get(`/users/admin/${id}`);
    return response.data.data;
  },
};
