from datetime import datetime from decimal import Decimal from sqlalchemy import DateTime, ForeignKey, Numeric, String, BigInteger, Text, Integer, func from sqlalchemy.orm import Mapped, mapped_column, relationship from app.core.database import Base class User(Base): __tablename__ = "users" id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) telegram_id: Mapped[int | None] = mapped_column(BigInteger, unique=True, index=True, nullable=True) username: Mapped[str | None] = mapped_column(String(255), nullable=True) first_name: Mapped[str | None] = mapped_column(String(255), nullable=True) balance: Mapped[Decimal] = mapped_column(Numeric(18, 2), default=Decimal("0.00"), nullable=False) agreement_accepted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False, ) deposits: Mapped[list["Deposit"]] = relationship(back_populates="user", cascade="all, delete-orphan") spin_sessions: Mapped[list["SpinSession"]] = relationship( back_populates="user", cascade="all, delete-orphan" ) class Deposit(Base): __tablename__ = "deposits" id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True, nullable=False) amount: Mapped[Decimal] = mapped_column(Numeric(18, 2), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) user: Mapped[User] = relationship(back_populates="deposits") class SpinSession(Base): __tablename__ = "spin_sessions" id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True, nullable=False) slug: Mapped[str] = mapped_column(String(64), nullable=False, index=True) bet: Mapped[Decimal] = mapped_column(Numeric(18, 2), nullable=False) spin_count: Mapped[int] = mapped_column(Integer, nullable=False) total_win: Mapped[Decimal] = mapped_column(Numeric(18, 2), nullable=False) payload_json: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) user: Mapped[User] = relationship(back_populates="spin_sessions")