import { apiClient as api } from './client'
import type {
  TierInfo,
  UserStats,
  UserAchievement,
  LootBox,
  LootBoxOpenResult,
  ActivityFeedItem,
  LeaderboardEntry,
  WhaleBet,
  RecentWin,
  DailyReward,
} from '../types/gamification'

const PREFIX = '/api/v1/gamification'

// Public endpoints
export async function getLeaderboard(
  category: 'profit' | 'wagered' | 'wins' | 'streak' | 'xp' = 'profit',
  limit: number = 20
): Promise<LeaderboardEntry[]> {
  const response = await api.get(`${PREFIX}/leaderboard/${category}`, {
    params: { limit },
  })
  return response.data
}

export async function getWhaleBets(limit: number = 20): Promise<WhaleBet[]> {
  const response = await api.get(`${PREFIX}/whale-tracker`, {
    params: { limit },
  })
  return response.data
}

export async function getActivityFeed(limit: number = 50): Promise<ActivityFeedItem[]> {
  const response = await api.get(`${PREFIX}/activity-feed`, {
    params: { limit },
  })
  return response.data
}

export async function getRecentWins(limit: number = 20): Promise<RecentWin[]> {
  const response = await api.get(`${PREFIX}/recent-wins`, {
    params: { limit },
  })
  return response.data
}

export async function getTierInfo(): Promise<TierInfo[]> {
  const response = await api.get(`${PREFIX}/tier-info`)
  return response.data
}

// Authenticated endpoints
export async function getMyStats(): Promise<UserStats> {
  const response = await api.get(`${PREFIX}/my-stats`)
  return response.data
}

export async function getMyAchievements(): Promise<UserAchievement[]> {
  const response = await api.get(`${PREFIX}/my-achievements`)
  return response.data
}

export async function getMyLootBoxes(): Promise<LootBox[]> {
  const response = await api.get(`${PREFIX}/my-loot-boxes`)
  return response.data
}

export async function openLootBox(lootBoxId: number): Promise<LootBoxOpenResult> {
  const response = await api.post(`${PREFIX}/my-loot-boxes/${lootBoxId}/open`)
  return response.data
}

export async function getDailyReward(): Promise<DailyReward | { available: boolean; next_available: string }> {
  const response = await api.get(`${PREFIX}/daily-reward`)
  return response.data
}

export async function claimDailyReward(): Promise<DailyReward> {
  const response = await api.post(`${PREFIX}/daily-reward/claim`)
  return response.data
}
