from __future__ import annotations import uuid from datetime import datetime from sqlalchemy import DateTime, ForeignKey, String, func from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column from app.db.base_class import Base class UserLoginSession(Base): """Server-side login activity record without storing credentials or raw IPs.""" __tablename__ = "user_login_sessions" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True) user_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True, ) client_type: Mapped[str] = mapped_column(String(16), nullable=False, default="web") client_platform: Mapped[str | None] = mapped_column(String(32), nullable=True) client_version: Mapped[str | None] = mapped_column(String(64), nullable=True) client_source: Mapped[str | None] = mapped_column(String(32), nullable=True) login_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now()) last_seen_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now()) ended_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) end_reason: Mapped[str | None] = mapped_column(String(32), nullable=True)