from datetime import datetime

from app.schemas.base import CamelModel
from app.schemas.game import GameResponse
from app.schemas.pokemon import PokemonResponse


class GenlockeCreate(CamelModel):
    name: str
    game_ids: list[int]
    genlocke_rules: dict = {}
    nuzlocke_rules: dict = {}


class GenlockeUpdate(CamelModel):
    name: str | None = None
    status: str | None = None


class AddLegRequest(CamelModel):
    game_id: int


class GenlockeLegResponse(CamelModel):
    id: int
    genlocke_id: int
    game_id: int
    run_id: int | None = None
    leg_order: int
    retired_pokemon_ids: list[int] | None = None
    game: GameResponse


class SurvivorResponse(CamelModel):
    id: int
    pokemon: PokemonResponse
    current_pokemon: PokemonResponse | None = None
    nickname: str | None = None
    catch_level: int | None = None
    is_shiny: bool = False
    route_name: str


class AdvanceLegRequest(CamelModel):
    transfer_encounter_ids: list[int] = []


class GenlockeResponse(CamelModel):
    id: int
    name: str
    status: str
    genlocke_rules: dict
    nuzlocke_rules: dict
    created_at: datetime
    legs: list[GenlockeLegResponse] = []


# --- List / Detail schemas ---


class RetiredPokemonResponse(CamelModel):
    id: int
    name: str
    sprite_url: str | None = None


class GenlockeLegDetailResponse(CamelModel):
    id: int
    leg_order: int
    game: GameResponse
    run_id: int | None = None
    run_status: str | None = None
    encounter_count: int = 0
    death_count: int = 0
    retired_pokemon_ids: list[int] | None = None


class GenlockeStatsResponse(CamelModel):
    total_encounters: int
    total_deaths: int
    legs_completed: int
    total_legs: int


class GenlockeListItem(CamelModel):
    id: int
    name: str
    status: str
    created_at: datetime
    total_legs: int
    completed_legs: int
    current_leg_order: int | None = None


class GenlockeDetailResponse(CamelModel):
    id: int
    name: str
    status: str
    genlocke_rules: dict
    nuzlocke_rules: dict
    created_at: datetime
    legs: list[GenlockeLegDetailResponse] = []
    stats: GenlockeStatsResponse
    retired_pokemon: dict[int, RetiredPokemonResponse] = {}


# --- Graveyard schemas ---


class GraveyardEntryResponse(CamelModel):
    id: int
    pokemon: PokemonResponse
    current_pokemon: PokemonResponse | None = None
    nickname: str | None = None
    catch_level: int | None = None
    faint_level: int | None = None
    death_cause: str | None = None
    is_shiny: bool = False
    route_name: str
    leg_order: int
    game_name: str


class GraveyardLegSummary(CamelModel):
    leg_order: int
    game_name: str
    death_count: int


class GenlockeGraveyardResponse(CamelModel):
    entries: list[GraveyardEntryResponse]
    total_deaths: int
    deaths_per_leg: list[GraveyardLegSummary]
    deadliest_leg: GraveyardLegSummary | None = None


# --- Lineage schemas ---


class LineageLegEntry(CamelModel):
    leg_order: int
    game_name: str
    encounter_id: int
    pokemon: PokemonResponse
    current_pokemon: PokemonResponse | None = None
    nickname: str | None = None
    catch_level: int | None = None
    faint_level: int | None = None
    death_cause: str | None = None
    is_shiny: bool = False
    route_name: str
    is_alive: bool
    entered_hof: bool
    was_transferred: bool


class LineageEntry(CamelModel):
    nickname: str | None
    pokemon: PokemonResponse  # base form from first leg
    legs: list[LineageLegEntry]
    status: str  # "alive" | "dead"


class GenlockeLineageResponse(CamelModel):
    lineages: list[LineageEntry]
    total_lineages: int
