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

from app.core.database import Base


class ActivityType(str, Enum):
    JOIN = "join"
    SPIN = "spin"
    COMPLETE = "complete"
    DROP = "drop"
    START_MARATHON = "start_marathon"
    FINISH_MARATHON = "finish_marathon"
    ADD_GAME = "add_game"
    APPROVE_GAME = "approve_game"
    REJECT_GAME = "reject_game"
    EVENT_START = "event_start"
    EVENT_END = "event_end"
    SWAP = "swap"
    GAME_CHOICE = "game_choice"


class Activity(Base):
    __tablename__ = "activities"

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

    # Relationships
    marathon: Mapped["Marathon"] = relationship("Marathon", back_populates="activities")
    user: Mapped["User"] = relationship("User")
