export type GameCategory = 'original' | 'remake' | 'enhanced' | 'sequel' | 'spinoff'

export interface Game {
  id: number
  name: string
  slug: string
  generation: number
  region: string
  category: GameCategory | null
  boxArtUrl: string | null
  releaseYear: number | null
  color: string | null
  versionGroupId: number | null
}

export interface GenlockeDefaults {
  trueGenlocke: string  // game slug
  normalGenlocke: string  // game slug
}

export interface Region {
  name: string
  generation: number
  order: number
  genlockeDefaults: GenlockeDefaults
  games: Game[]
}

export interface Route {
  id: number
  name: string
  versionGroupId: number
  order: number
  parentRouteId: number | null
  pinwheelZone: number | null
  encounterMethods: string[]
}

export interface RouteWithChildren extends Route {
  children: Route[]
}

export interface Pokemon {
  id: number
  pokeapiId: number
  nationalDex: number
  name: string
  types: string[]
  spriteUrl: string | null
}

export interface RouteEncounter {
  id: number
  routeId: number
  pokemonId: number
  gameId: number
  encounterMethod: string
  encounterRate: number
  minLevel: number
  maxLevel: number
}

export interface RouteEncounterDetail extends RouteEncounter {
  pokemon: Pokemon
}

export type EncounterStatus = 'caught' | 'fainted' | 'missed'

export interface Encounter {
  id: number
  runId: number
  routeId: number
  pokemonId: number
  currentPokemonId: number | null
  nickname: string | null
  status: EncounterStatus
  catchLevel: number | null
  faintLevel: number | null
  deathCause: string | null
  isShiny: boolean
  caughtAt: string
}

export type RunStatus = 'active' | 'completed' | 'failed'

export interface NuzlockeRun {
  id: number
  gameId: number
  name: string
  status: RunStatus
  rules: NuzlockeRules
  hofEncounterIds: number[] | null
  startedAt: string
  completedAt: string | null
}

export interface RunGenlockeContext {
  genlockeId: number
  genlockeName: string
  legOrder: number
  totalLegs: number
  isFinalLeg: boolean
  retiredPokemonIds: number[]
}

export interface RunDetail extends NuzlockeRun {
  game: Game
  encounters: EncounterDetail[]
  genlocke: RunGenlockeContext | null
  transferEncounterIds: number[]
}

export interface EncounterDetail extends Encounter {
  pokemon: Pokemon
  currentPokemon: Pokemon | null
  route: Route
}

export interface Evolution {
  id: number
  fromPokemonId: number
  toPokemon: Pokemon
  trigger: string
  minLevel: number | null
  item: string | null
  heldItem: string | null
  condition: string | null
  region: string | null
}

export interface CreateRunInput {
  gameId: number
  name: string
  rules?: NuzlockeRules
}

export interface UpdateRunInput {
  name?: string
  status?: RunStatus
  rules?: NuzlockeRules
  hofEncounterIds?: number[]
}

export interface CreateEncounterInput {
  routeId: number
  pokemonId: number
  nickname?: string
  status: EncounterStatus
  catchLevel?: number
  isShiny?: boolean
  origin?: string
}

export interface UpdateEncounterInput {
  nickname?: string
  status?: EncounterStatus
  faintLevel?: number
  deathCause?: string
  currentPokemonId?: number
}

// Boss battles
export type BossType = 'gym_leader' | 'elite_four' | 'champion' | 'rival' | 'evil_team' | 'other'

export interface BossPokemon {
  id: number
  pokemonId: number
  level: number
  order: number
  conditionLabel: string | null
  pokemon: Pokemon
}

export interface BossBattle {
  id: number
  versionGroupId: number
  name: string
  bossType: BossType
  specialtyType: string | null
  badgeName: string | null
  badgeImageUrl: string | null
  levelCap: number
  order: number
  afterRouteId: number | null
  location: string
  section: string | null
  spriteUrl: string | null
  pokemon: BossPokemon[]
}

export interface BossResult {
  id: number
  runId: number
  bossBattleId: number
  result: 'won' | 'lost'
  attempts: number
  completedAt: string | null
}

export interface CreateBossResultInput {
  bossBattleId: number
  result: 'won' | 'lost'
  attempts?: number
}

// Re-export for convenience
import type { NuzlockeRules } from './rules'
export type { NuzlockeRules }

// Genlocke types
export interface GenlockeRules {
  retireHoF: boolean
}

export interface GenlockeLeg {
  id: number
  genlockeId: number
  gameId: number
  runId: number | null
  legOrder: number
  game: Game
}

export interface Genlocke {
  id: number
  name: string
  status: 'active' | 'completed' | 'failed'
  genlockeRules: GenlockeRules
  nuzlockeRules: NuzlockeRules
  createdAt: string
  legs: GenlockeLeg[]
}

export interface CreateGenlockeInput {
  name: string
  gameIds: number[]
  genlockeRules: GenlockeRules
  nuzlockeRules: NuzlockeRules
}

// Genlocke list / detail types

export interface GenlockeLegDetail {
  id: number
  legOrder: number
  game: Game
  runId: number | null
  runStatus: RunStatus | null
  encounterCount: number
  deathCount: number
  retiredPokemonIds: number[] | null
}

export interface GenlockeStats {
  totalEncounters: number
  totalDeaths: number
  legsCompleted: number
  totalLegs: number
}

export interface GenlockeListItem {
  id: number
  name: string
  status: 'active' | 'completed' | 'failed'
  createdAt: string
  totalLegs: number
  completedLegs: number
  currentLegOrder: number | null
}

export interface RetiredPokemon {
  id: number
  name: string
  spriteUrl: string | null
}

export interface GenlockeDetail {
  id: number
  name: string
  status: 'active' | 'completed' | 'failed'
  genlockeRules: GenlockeRules
  nuzlockeRules: NuzlockeRules
  createdAt: string
  legs: GenlockeLegDetail[]
  stats: GenlockeStats
  retiredPokemon: Record<number, RetiredPokemon>
}

// Transfer types

export interface SurvivorEncounter {
  id: number
  pokemon: Pokemon
  currentPokemon: Pokemon | null
  nickname: string | null
  catchLevel: number | null
  isShiny: boolean
  routeName: string
}

export interface AdvanceLegInput {
  transferEncounterIds: number[]
}

// Lineage types

export interface LineageLegEntry {
  legOrder: number
  gameName: string
  encounterId: number
  pokemon: Pokemon
  currentPokemon: Pokemon | null
  nickname: string | null
  catchLevel: number | null
  faintLevel: number | null
  deathCause: string | null
  isShiny: boolean
  routeName: string
  isAlive: boolean
  enteredHof: boolean
  wasTransferred: boolean
}

export interface LineageEntry {
  nickname: string | null
  pokemon: Pokemon
  legs: LineageLegEntry[]
  status: 'alive' | 'dead'
}

export interface GenlockeLineage {
  lineages: LineageEntry[]
  totalLineages: number
}

// Graveyard types

export interface GraveyardEntry {
  id: number
  pokemon: Pokemon
  currentPokemon: Pokemon | null
  nickname: string | null
  catchLevel: number | null
  faintLevel: number | null
  deathCause: string | null
  isShiny: boolean
  routeName: string
  legOrder: number
  gameName: string
}

export interface GraveyardLegSummary {
  legOrder: number
  gameName: string
  deathCount: number
}

export interface GenlockeGraveyard {
  entries: GraveyardEntry[]
  totalDeaths: number
  deathsPerLeg: GraveyardLegSummary[]
  deadliestLeg: GraveyardLegSummary | null
}
