import { api } from './client'
import type {
  Game,
  Route,
  Pokemon,
  RouteEncounterDetail,
  BossBattle,
  CreateGameInput,
  UpdateGameInput,
  CreateRouteInput,
  UpdateRouteInput,
  RouteReorderItem,
  CreatePokemonInput,
  UpdatePokemonInput,
  BulkImportResult,
  PaginatedPokemon,
  CreateRouteEncounterInput,
  UpdateRouteEncounterInput,
  EvolutionAdmin,
  PaginatedEvolutions,
  CreateEvolutionInput,
  UpdateEvolutionInput,
  CreateBossBattleInput,
  UpdateBossBattleInput,
  BossPokemonInput,
  BossReorderItem,
  UpdateGenlockeInput,
  AddGenlockeLegInput,
} from '../types'
import type { Genlocke } from '../types/game'

// Games
export const createGame = (data: CreateGameInput) =>
  api.post<Game>('/games', data)

export const updateGame = (id: number, data: UpdateGameInput) =>
  api.put<Game>(`/games/${id}`, data)

export const deleteGame = (id: number) =>
  api.del(`/games/${id}`)

// Routes
export const createRoute = (gameId: number, data: CreateRouteInput) =>
  api.post<Route>(`/games/${gameId}/routes`, data)

export const updateRoute = (gameId: number, routeId: number, data: UpdateRouteInput) =>
  api.put<Route>(`/games/${gameId}/routes/${routeId}`, data)

export const deleteRoute = (gameId: number, routeId: number) =>
  api.del(`/games/${gameId}/routes/${routeId}`)

export const reorderRoutes = (gameId: number, routes: RouteReorderItem[]) =>
  api.put<Route[]>(`/games/${gameId}/routes/reorder`, { routes })

// Pokemon
export const listPokemon = (search?: string, limit = 50, offset = 0, type?: string) => {
  const params = new URLSearchParams()
  if (search) params.set('search', search)
  if (type) params.set('type', type)
  params.set('limit', String(limit))
  params.set('offset', String(offset))
  return api.get<PaginatedPokemon>(`/pokemon?${params}`)
}

export const createPokemon = (data: CreatePokemonInput) =>
  api.post<Pokemon>('/pokemon', data)

export const updatePokemon = (id: number, data: UpdatePokemonInput) =>
  api.put<Pokemon>(`/pokemon/${id}`, data)

export const deletePokemon = (id: number) =>
  api.del(`/pokemon/${id}`)

export const bulkImportPokemon = (items: Array<{ pokeapiId: number; nationalDex: number; name: string; types: string[]; spriteUrl?: string | null }>) =>
  api.post<BulkImportResult>('/pokemon/bulk-import', items)

export const bulkImportEvolutions = (items: unknown[]) =>
  api.post<BulkImportResult>('/evolutions/bulk-import', items)

export const bulkImportRoutes = (gameId: number, items: unknown[]) =>
  api.post<BulkImportResult>(`/games/${gameId}/routes/bulk-import`, items)

export const bulkImportBosses = (gameId: number, items: unknown[]) =>
  api.post<BulkImportResult>(`/games/${gameId}/bosses/bulk-import`, items)

// Evolutions
export const listEvolutions = (search?: string, limit = 50, offset = 0, trigger?: string) => {
  const params = new URLSearchParams()
  if (search) params.set('search', search)
  if (trigger) params.set('trigger', trigger)
  params.set('limit', String(limit))
  params.set('offset', String(offset))
  return api.get<PaginatedEvolutions>(`/evolutions?${params}`)
}

export const createEvolution = (data: CreateEvolutionInput) =>
  api.post<EvolutionAdmin>('/evolutions', data)

export const updateEvolution = (id: number, data: UpdateEvolutionInput) =>
  api.put<EvolutionAdmin>(`/evolutions/${id}`, data)

export const deleteEvolution = (id: number) =>
  api.del(`/evolutions/${id}`)

// Export
export const exportGames = () =>
  api.get<Record<string, unknown>[]>('/export/games')

export const exportGameRoutes = (gameId: number) =>
  api.get<{ filename: string; data: unknown }>(`/export/games/${gameId}/routes`)

export const exportGameBosses = (gameId: number) =>
  api.get<{ filename: string; data: unknown }>(`/export/games/${gameId}/bosses`)

export const exportPokemon = () =>
  api.get<Record<string, unknown>[]>('/export/pokemon')

export const exportEvolutions = () =>
  api.get<Record<string, unknown>[]>('/export/evolutions')

// Route Encounters
export const addRouteEncounter = (routeId: number, data: CreateRouteEncounterInput) =>
  api.post<RouteEncounterDetail>(`/routes/${routeId}/pokemon`, data)

export const updateRouteEncounter = (routeId: number, encounterId: number, data: UpdateRouteEncounterInput) =>
  api.put<RouteEncounterDetail>(`/routes/${routeId}/pokemon/${encounterId}`, data)

export const removeRouteEncounter = (routeId: number, encounterId: number) =>
  api.del(`/routes/${routeId}/pokemon/${encounterId}`)

// Boss Battles
export const createBossBattle = (gameId: number, data: CreateBossBattleInput) =>
  api.post<BossBattle>(`/games/${gameId}/bosses`, data)

export const updateBossBattle = (gameId: number, bossId: number, data: UpdateBossBattleInput) =>
  api.put<BossBattle>(`/games/${gameId}/bosses/${bossId}`, data)

export const deleteBossBattle = (gameId: number, bossId: number) =>
  api.del(`/games/${gameId}/bosses/${bossId}`)

export const reorderBosses = (gameId: number, bosses: BossReorderItem[]) =>
  api.put<BossBattle[]>(`/games/${gameId}/bosses/reorder`, { bosses })

export const setBossTeam = (gameId: number, bossId: number, team: BossPokemonInput[]) =>
  api.put<BossBattle>(`/games/${gameId}/bosses/${bossId}/pokemon`, team)

// Genlockes
export const updateGenlocke = (id: number, data: UpdateGenlockeInput) =>
  api.patch<Genlocke>(`/genlockes/${id}`, data)

export const deleteGenlocke = (id: number) =>
  api.del(`/genlockes/${id}`)

export const addGenlockeLeg = (genlockeId: number, data: AddGenlockeLegInput) =>
  api.post<Genlocke>(`/genlockes/${genlockeId}/legs`, data)

export const deleteGenlockeLeg = (genlockeId: number, legId: number) =>
  api.del(`/genlockes/${genlockeId}/legs/${legId}`)
