"""User Entity Model for Internal Subject Mapping. Fulfills Database Design & LLD-001 / LLD-002. Maps external Keycloak OIDC subjects (`sub`) to internal platform user records. """ from datetime import datetime, timezone import uuid from typing import Optional # Minimal database-independent representation compatible with SQLAlchemy try: from sqlalchemy import Column, String, Boolean, DateTime from sqlalchemy.orm import declarative_base Base = declarative_base() class User(Base): __tablename__ = "users" id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4())) subject = Column(String(128), unique=True, nullable=False, index=True) email = Column(String(255), unique=True, nullable=False, index=True) full_name = Column(String(255), nullable=True) is_active = Column(Boolean, default=True, nullable=False) created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False) updated_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), nullable=False) except ImportError: # Lightweight fallback model when SQLAlchemy is not loaded in current environment Base = None class User: def __init__(self, subject: str, email: str, full_name: Optional[str] = None, is_active: bool = True, id: Optional[str] = None): self.id = id or str(uuid.uuid4()) self.subject = subject self.email = email self.full_name = full_name self.is_active = is_active self.created_at = datetime.now(timezone.utc) self.updated_at = datetime.now(timezone.utc)