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:
    race_id: str
    scope: str
    champion_name: str
    champion_owner: str
    participants: list[str]
    bet_distribution: dict[str, int]
    duration_ticks: int
    completed_at: datetime
    point_changes: dict[str, int] = field(default_factory=dict)
    point_change_summaries: dict[str, str] = field(default_factory=dict)
