package models import ( "time" "gorm.io/gorm" ) // Conversation represents a chat conversation between users and/or agents type Conversation struct { ID uint `json:"id" gorm:"primaryKey"` Title string `json:"title"` UserID uint `json:"userId"` AgentID *uint `json:"agentId,omitempty"` Status string `json:"status" gorm:"default:'active'"` // 'active', 'closed', 'escalated' Department string `json:"department"` Priority string `json:"priority" gorm:"default:'medium'"` // 'low', 'medium', 'high', 'urgent' Tags string `json:"tags"` // Comma-separated tags LastMessageAt time.Time `json:"lastMessageAt"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // Relationships User User `json:"user" gorm:"foreignKey:UserID"` Agent *User `json:"agent,omitempty" gorm:"foreignKey:AgentID"` Messages []Message `json:"messages" gorm:"foreignKey:ConversationID"` } // Message represents a single message in a conversation type Message struct { ID uint `json:"id" gorm:"primaryKey"` ConversationID uint `json:"conversationId"` UserID uint `json:"userId"` Content string `json:"content" gorm:"not null"` Type string `json:"type" gorm:"default:'text'"` // 'text', 'image', 'file', 'system' Status string `json:"status" gorm:"default:'sent'"` // 'sent', 'delivered', 'read' Sentiment float64 `json:"sentiment"` // Sentiment score from -1 (negative) to 1 (positive) IsAI bool `json:"isAI" gorm:"default:false"` AIModel string `json:"aiModel,omitempty"` // Which AI model generated this message ReadAt *time.Time `json:"readAt,omitempty"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // Relationships Conversation Conversation `json:"conversation" gorm:"foreignKey:ConversationID"` User User `json:"user" gorm:"foreignKey:UserID"` } // BeforeCreate is a GORM hook that sets the LastMessageAt field when creating a conversation func (c *Conversation) BeforeCreate(tx *gorm.DB) error { c.LastMessageAt = time.Now() return nil } // BeforeCreate is a GORM hook that updates the conversation's LastMessageAt when creating a message func (m *Message) BeforeCreate(tx *gorm.DB) error { // Update the conversation's LastMessageAt tx.Model(&Conversation{}).Where("id = ?", m.ConversationID).Update("last_message_at", time.Now()) return nil }