""" Attachment model — files attached to tickets or Knowledge Base articles. """ from sqlalchemy import Column, String, ForeignKey, Integer, Text, Boolean, Index, Computed from sqlalchemy.dialects.postgresql import UUID, TSVECTOR from sqlalchemy.orm import relationship from pgvector.sqlalchemy import Vector from core.database import Base from core.config import get_settings from models.base import PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin settings = get_settings() class Attachment(Base, PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin): __tablename__ = "attachments" workspace_id = Column(UUID(as_uuid=True), ForeignKey("workspaces.id"), nullable=False, index=True) ticket_id = Column(UUID(as_uuid=True), ForeignKey("tickets.id"), nullable=True, index=True) article_id = Column(UUID(as_uuid=True), ForeignKey("kb_articles.id"), nullable=True, index=True) uploader_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True) filename = Column(String(500), nullable=False) storage_path = Column(String(1024), nullable=False) storage_backend = Column(String(20), default="local") # "local" or "s3" content_type = Column(String(100), nullable=True) file_size = Column(Integer, nullable=True) source_url = Column(String(2048), nullable=True) # For Markdown-extracted images # Multimodal AI Fields embedding = Column(Vector(settings.AI_IMAGE_EMBEDDING_DIM), nullable=True) # Vision Vector (e.g. CLIP) ai_caption = Column(Text, nullable=True) # AI-generated description is_processed = Column(Boolean, default=False, index=True) # --- Search Optimization --- # 1. Computed TSVector for GIN search search_vector = Column( TSVECTOR, Computed("to_tsvector('english', coalesce(ai_caption, ''))", persisted=True) ) # 2. GIN Index for visual forensic search __table_args__ = ( Index('idx_att_search_vector', "search_vector", postgresql_using='gin'), ) # Relationships ticket = relationship("Ticket", back_populates="attachments") article = relationship("KBArticle", back_populates="attachments")