from __future__ import annotations from datetime import UTC, datetime from uuid import uuid4 from sqlalchemy import DateTime, ForeignKey, Integer, String, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from app.db.base import Base class User(Base): __tablename__ = "users" id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4())) email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True) password_hash: Mapped[str] = mapped_column(String(255), nullable=False) role: Mapped[str] = mapped_column(String(16), nullable=False, default="user") is_superuser: Mapped[bool] = mapped_column(nullable=False, default=False) status: Mapped[str] = mapped_column(String(16), nullable=False, default="pending", index=True) failed_login_attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0) locked_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) email_verified_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC) ) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC), ) profile: Mapped["UserProfile"] = relationship( back_populates="user", uselist=False, cascade="all, delete-orphan", passive_deletes=True, ) class UserProfile(Base): __tablename__ = "user_profiles" user_id: Mapped[str] = mapped_column( String(36), ForeignKey("users.id", ondelete="CASCADE"), primary_key=True ) display_name: Mapped[str] = mapped_column(String(120), nullable=False) avatar_url: Mapped[str | None] = mapped_column(String(512), nullable=True) metadata_json: Mapped[str | None] = mapped_column(Text, nullable=True) user: Mapped[User] = relationship(back_populates="profile")