package main

import (
	"strconv"
	"strings"
)

// NamedRef is a PokeAPI named resource reference (name + URL).
type NamedRef struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

// ID extracts the trailing integer ID from the URL path.
// e.g. "https://pokeapi.co/api/v2/pokemon/25/" -> 25
func (r NamedRef) ID() int {
	s := strings.TrimRight(r.URL, "/")
	parts := strings.Split(s, "/")
	id, _ := strconv.Atoi(parts[len(parts)-1])
	return id
}

// ---- PokeAPI response structs (only fields we use) ----

type SpeciesListResp struct {
	Results []NamedRef `json:"results"`
}

type RegionResp struct {
	Locations []NamedRef `json:"locations"`
}

type LocationResp struct {
	Areas []NamedRef `json:"areas"`
}

type LocationAreaResp struct {
	PokemonEncounters []PokemonEncounter `json:"pokemon_encounters"`
}

type PokemonEncounter struct {
	Pokemon        NamedRef        `json:"pokemon"`
	VersionDetails []VersionDetail `json:"version_details"`
}

type VersionDetail struct {
	Version          NamedRef          `json:"version"`
	EncounterDetails []EncounterDetail `json:"encounter_details"`
}

type EncounterDetail struct {
	Chance   int      `json:"chance"`
	Method   NamedRef `json:"method"`
	MinLevel int      `json:"min_level"`
	MaxLevel int      `json:"max_level"`
}

type SpeciesResp struct {
	ID             int      `json:"id"`
	Name           string   `json:"name"`
	EvolutionChain NamedRef `json:"evolution_chain"`
	Varieties      []struct {
		IsDefault bool     `json:"is_default"`
		Pokemon   NamedRef `json:"pokemon"`
	} `json:"varieties"`
}

type PokemonResp struct {
	ID      int      `json:"id"`
	Name    string   `json:"name"`
	Species NamedRef `json:"species"`
	Types   []struct {
		Slot int `json:"slot"`
		Type struct {
			Name string `json:"name"`
		} `json:"type"`
	} `json:"types"`
}

type EvolutionChainResp struct {
	ID    int       `json:"id"`
	Chain ChainLink `json:"chain"`
}

type ChainLink struct {
	Species          NamedRef          `json:"species"`
	EvolvesTo        []ChainLink       `json:"evolves_to"`
	EvolutionDetails []EvolutionDetail `json:"evolution_details"`
}

type EvolutionDetail struct {
	Trigger               NamedRef  `json:"trigger"`
	MinLevel              *int      `json:"min_level"`
	Item                  *NamedRef `json:"item"`
	HeldItem              *NamedRef `json:"held_item"`
	MinHappiness          *int      `json:"min_happiness"`
	MinAffection          *int      `json:"min_affection"`
	MinBeauty             *int      `json:"min_beauty"`
	TimeOfDay             string    `json:"time_of_day"`
	KnownMove             *NamedRef `json:"known_move"`
	KnownMoveType         *NamedRef `json:"known_move_type"`
	Location              *NamedRef `json:"location"`
	PartySpecies          *NamedRef `json:"party_species"`
	PartyType             *NamedRef `json:"party_type"`
	Gender                *int      `json:"gender"`
	NeedsOverworldRain    bool      `json:"needs_overworld_rain"`
	TurnUpsideDown        bool      `json:"turn_upside_down"`
	TradeSpecies          *NamedRef `json:"trade_species"`
	RelativePhysicalStats *int      `json:"relative_physical_stats"`
}
