from datetime import datetime
from enum import Enum
from sqlalchemy import String, DateTime, ForeignKey, JSON, Boolean
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.database import Base


class EventType(str, Enum):
    GOLDEN_HOUR = "golden_hour"      # x1.5 очков
    COMMON_ENEMY = "common_enemy"    # общий челлендж для всех
    DOUBLE_RISK = "double_risk"      # дропы бесплатны, x0.5 очков
    JACKPOT = "jackpot"              # x3 за сложный челлендж
    SWAP = "swap"                    # обмен заданиями
    GAME_CHOICE = "game_choice"      # выбор игры (2-3 челленджа на выбор)


class Event(Base):
    __tablename__ = "events"

    id: Mapped[int] = mapped_column(primary_key=True)
    marathon_id: Mapped[int] = mapped_column(
        ForeignKey("marathons.id", ondelete="CASCADE"),
        index=True
    )
    type: Mapped[str] = mapped_column(String(30), nullable=False)
    start_time: Mapped[datetime] = mapped_column(DateTime, nullable=False)
    end_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    created_by_id: Mapped[int | None] = mapped_column(
        ForeignKey("users.id", ondelete="SET NULL"),
        nullable=True
    )
    data: Mapped[dict | None] = mapped_column(JSON, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    # Relationships
    marathon: Mapped["Marathon"] = relationship("Marathon", back_populates="events")
    created_by: Mapped["User | None"] = relationship("User")
    assignments: Mapped[list["Assignment"]] = relationship("Assignment", back_populates="event")
