from sqlalchemy import ForeignKey, String, DateTime, Enum, Integer, Float
from sqlalchemy.orm import Mapped, mapped_column, relationship
from datetime import datetime
import enum
from app.database import Base


class SportType(enum.Enum):
    FOOTBALL = "football"
    BASKETBALL = "basketball"
    BASEBALL = "baseball"
    HOCKEY = "hockey"
    SOCCER = "soccer"


class EventStatus(enum.Enum):
    UPCOMING = "upcoming"
    LIVE = "live"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


class SportEvent(Base):
    """Admin-created sporting events for spread betting"""
    __tablename__ = "sport_events"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)

    # Event details
    sport: Mapped[SportType] = mapped_column(Enum(SportType))
    home_team: Mapped[str] = mapped_column(String(100))
    away_team: Mapped[str] = mapped_column(String(100))

    # Spread (positive = home team favored, negative = away team favored)
    # Example: +3 means home team is 3-point favorite
    official_spread: Mapped[float] = mapped_column(Float)

    # Game info
    game_time: Mapped[datetime] = mapped_column(DateTime)
    venue: Mapped[str | None] = mapped_column(String(200), nullable=True)
    league: Mapped[str | None] = mapped_column(String(100), nullable=True)

    # Status
    status: Mapped[EventStatus] = mapped_column(Enum(EventStatus), default=EventStatus.UPCOMING)

    # Final scores (set when completed)
    final_score_home: Mapped[int | None] = mapped_column(Integer, nullable=True)
    final_score_away: Mapped[int | None] = mapped_column(Integer, nullable=True)

    # Spread range available for betting
    min_spread: Mapped[float] = mapped_column(Float, default=-10.0)
    max_spread: Mapped[float] = mapped_column(Float, default=10.0)

    # Betting limits
    min_bet_amount: Mapped[float] = mapped_column(Float, default=10.0)
    max_bet_amount: Mapped[float] = mapped_column(Float, default=1000.0)

    # Admin who created this event
    created_by: Mapped[int] = mapped_column(ForeignKey("users.id"))

    # Timestamps
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # Relationships
    creator: Mapped["User"] = relationship(foreign_keys=[created_by])
    spread_bets: Mapped[list["SpreadBet"]] = relationship(back_populates="event")
