package repository import ( "context" "github.com/gochat/gochat/internal/model" "gorm.io/gorm" ) // CopilotThreadRepo provides data access for CopilotThread. type CopilotThreadRepo struct { db *gorm.DB } func NewCopilotThreadRepo(db *gorm.DB) *CopilotThreadRepo { return &CopilotThreadRepo{db: db} } func (r *CopilotThreadRepo) Create(ctx context.Context, thread *model.CopilotThread) error { return r.db.WithContext(ctx).Create(thread).Error } func (r *CopilotThreadRepo) GetByID(ctx context.Context, id uint) (*model.CopilotThread, error) { var thread model.CopilotThread if err := r.db.WithContext(ctx).Preload("Messages").First(&thread, id).Error; err != nil { return nil, err } return &thread, nil } func (r *CopilotThreadRepo) Update(ctx context.Context, thread *model.CopilotThread) error { return r.db.WithContext(ctx).Save(thread).Error } func (r *CopilotThreadRepo) Delete(ctx context.Context, id uint) error { return r.db.WithContext(ctx).Delete(&model.CopilotThread{}, id).Error } func (r *CopilotThreadRepo) ListByUser(ctx context.Context, accountID, userID uint, offset, limit int) ([]model.CopilotThread, int64, error) { var threads []model.CopilotThread var count int64 db := r.db.WithContext(ctx).Model(&model.CopilotThread{}).Where("account_id = ? AND user_id = ?", accountID, userID) db.Count(&count) if err := db.Offset(offset).Limit(limit).Find(&threads).Error; err != nil { return nil, 0, err } return threads, count, nil } // FindByAccountID retrieves all threads for a given account with pagination. func (r *CopilotThreadRepo) FindByAccountID(ctx context.Context, accountID uint, offset, limit int) ([]model.CopilotThread, int64, error) { var threads []model.CopilotThread var count int64 db := r.db.WithContext(ctx).Model(&model.CopilotThread{}).Where("account_id = ?", accountID) db.Count(&count) if err := db.Offset(offset).Limit(limit).Find(&threads).Error; err != nil { return nil, 0, err } return threads, count, nil } // FindByAssistantID retrieves all threads linked to a given assistant. func (r *CopilotThreadRepo) FindByAssistantID(ctx context.Context, assistantID uint, offset, limit int) ([]model.CopilotThread, int64, error) { var threads []model.CopilotThread var count int64 db := r.db.WithContext(ctx).Model(&model.CopilotThread{}).Where("assistant_id = ?", assistantID) db.Count(&count) if err := db.Offset(offset).Limit(limit).Find(&threads).Error; err != nil { return nil, 0, err } return threads, count, nil }