from dataclasses import dataclass, field
from enum import Enum
from datetime import datetime
from typing import Optional


class RoomState(str, Enum):
    WAITING = "waiting"
    RUNNING = "running"
    FINISHED = "finished"
    INTERRUPTED = "interrupted"


class HorseState(str, Enum):
    READY = "ready"
    RACING = "racing"
    FINISHED = "finished"


@dataclass
class Horse:
    owner_id: str
    name: str
    index: int = 0
    position: float = 0.0
    state: HorseState = HorseState.READY


@dataclass
class Bet:
    user_id: str
    horse_name: str
    amount: int


@dataclass
class Room:
    scope: str
    state: RoomState = RoomState.WAITING
    created_at: datetime = field(default_factory=datetime.now)
    horses: dict[str, Horse] = field(default_factory=dict)
    bets: list[Bet] = field(default_factory=list)
    champion_name: Optional[str] = None
    tick_count: int = 0
    next_horse_index: int = 1


@dataclass
class RaceResult:
    champion_name: str
    champion_owner: str
    point_changes: dict[str, int] = field(default_factory=dict)
    point_change_summaries: dict[str, str] = field(default_factory=dict)
    race_id: str = ""
    scope: str = ""
    participants: list[str] = field(default_factory=list)
    bet_distribution: dict[str, int] = field(default_factory=dict)
    duration_ticks: int = 0
    completed_at: datetime = field(default_factory=datetime.now)
    odds_snapshot: dict[str, float] = field(default_factory=dict)
