import uuid from sqlalchemy import Column, String, Boolean, DateTime, func, Index from sqlalchemy.dialects.postgresql import UUID from auth.database import Base # ========================================================= # USERS TABLE (CORE AUTH ENTITY) # ========================================================= class User(Base): __tablename__ = "users" # ----------------------------- # Primary Key (UUID for Supabase compatibility) # ----------------------------- id = Column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, nullable=False, ) # ----------------------------- # Identity Fields # ----------------------------- email = Column(String(255), unique=True, nullable=False, index=True) username = Column(String(100), unique=True, nullable=True, index=True) # ----------------------------- # Security Fields # NOTE: stores hashed password only (never plaintext) # ----------------------------- hashed_password = Column(String(255), nullable=False) # ----------------------------- # Account State # ----------------------------- is_active = Column(Boolean, default=True, nullable=False) is_verified = Column(Boolean, default=False, nullable=False) # ----------------------------- # Audit Fields # ----------------------------- created_at = Column( DateTime(timezone=True), server_default=func.now(), nullable=False, ) updated_at = Column( DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False, ) # ========================================================= # OPTIONAL: API KEY TABLE (FOR AUTOMATION / N8N / WORKFLOWS) # ========================================================= class ApiKey(Base): __tablename__ = "api_keys" id = Column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, nullable=False, ) user_id = Column( UUID(as_uuid=True), nullable=False, index=True, ) key_hash = Column(String(255), nullable=False, unique=True) name = Column(String(120), nullable=True) is_active = Column(Boolean, default=True, nullable=False) created_at = Column( DateTime(timezone=True), server_default=func.now(), nullable=False, ) # ========================================================= # INDEXES (PERFORMANCE OPTIMIZATION) # ========================================================= Index("idx_users_email", User.email) Index("idx_users_username", User.username) Index("idx_api_keys_user_id", ApiKey.user_id)