package repository import ( "context" "gorm.io/gorm" "github.com/gochat/gochat/internal/model" ) // PlatformAppRepo provides data access for PlatformApp. // Reference: Chatwoot app/models/platform_app.rb + app/models/agent_bot.rb type PlatformAppRepo struct { db *gorm.DB } // NewPlatformAppRepo creates a new PlatformApp repository. func NewPlatformAppRepo(db *gorm.DB) *PlatformAppRepo { return &PlatformAppRepo{db: db} } // Create inserts a new PlatformApp record. func (r *PlatformAppRepo) Create(ctx context.Context, app *model.PlatformApp) error { return r.db.WithContext(ctx).Create(app).Error } // GetByID retrieves a PlatformApp by primary key. func (r *PlatformAppRepo) GetByID(ctx context.Context, id uint) (*model.PlatformApp, error) { var app model.PlatformApp if err := r.db.WithContext(ctx).First(&app, id).Error; err != nil { return nil, err } return &app, nil } // GetByIDWithRelations retrieves a PlatformApp by primary key with Permissibles and AccessToken loaded. func (r *PlatformAppRepo) GetByIDWithRelations(ctx context.Context, id uint) (*model.PlatformApp, error) { var app model.PlatformApp if err := r.db.WithContext(ctx). Preload("Permissibles"). Preload("AccessToken", "owner_type = ?", model.AccessTokenOwnerTypePlatformApp). First(&app, id).Error; err != nil { return nil, err } return &app, nil } // Update saves changes to an existing PlatformApp. func (r *PlatformAppRepo) Update(ctx context.Context, app *model.PlatformApp) error { return r.db.WithContext(ctx).Save(app).Error } // Delete soft-deletes a PlatformApp by ID (uses gorm.DeletedAt). func (r *PlatformAppRepo) Delete(ctx context.Context, id uint) error { return r.db.WithContext(ctx).Delete(&model.PlatformApp{}, id).Error } // FindAll retrieves all PlatformApps with pagination. // Used by super-admin platform routes (no account filter). func (r *PlatformAppRepo) FindAll(ctx context.Context, offset, limit int) ([]model.PlatformApp, int64, error) { var apps []model.PlatformApp var count int64 db := r.db.WithContext(ctx).Model(&model.PlatformApp{}) db.Count(&count) if err := db.Offset(offset).Limit(limit).Order("id ASC").Find(&apps).Error; err != nil { return nil, 0, err } return apps, count, nil } // FindByAccountID retrieves PlatformApps scoped to an account with pagination. // Used by account-level routes. func (r *PlatformAppRepo) FindByAccountID(ctx context.Context, accountID uint, offset, limit int) ([]model.PlatformApp, int64, error) { var apps []model.PlatformApp var count int64 db := r.db.WithContext(ctx).Model(&model.PlatformApp{}).Where("account_id = ?", accountID) db.Count(&count) if err := db.Offset(offset).Limit(limit).Order("id ASC").Find(&apps).Error; err != nil { return nil, 0, err } return apps, count, nil } // FindByTokenPrefix retrieves a PlatformApp by AccessToken prefix. // Used for AccessToken authentication — lookup by prefix first, then verify hash. func (r *PlatformAppRepo) FindByTokenPrefix(ctx context.Context, prefix string) (*model.PlatformApp, error) { var token model.AccessToken if err := r.db.WithContext(ctx). Where("token_prefix = ? AND owner_type = ? AND deleted_at IS NULL", prefix, model.AccessTokenOwnerTypePlatformApp). First(&token).Error; err != nil { return nil, err } return r.GetByID(ctx, token.OwnerID) } // CreateAccessToken creates an AccessToken for a PlatformApp. func (r *PlatformAppRepo) CreateAccessToken(ctx context.Context, token *model.AccessToken) error { return r.db.WithContext(ctx).Create(token).Error } // SearchByName searches PlatformApps by name with LIKE pattern matching. // Reference: Chatwoot PlatformApp search/filter (super admin). // Uses LIKE (not ILIKE) for SQLite compatibility; PG tests can use ILIKE via dialect override. func (r *PlatformAppRepo) SearchByName(ctx context.Context, nameQuery string, offset, limit int) ([]model.PlatformApp, int64, error) { var apps []model.PlatformApp var count int64 likeQuery := "%" + nameQuery + "%" db := r.db.WithContext(ctx).Model(&model.PlatformApp{}). Where("name LIKE ?", likeQuery) db.Count(&count) if err := db.Offset(offset).Limit(limit).Order("id ASC").Find(&apps).Error; err != nil { return nil, 0, err } return apps, count, nil } // SearchByNameByAccount searches PlatformApps by name scoped to an account. // Reference: Chatwoot account-level platform app search. // Uses LIKE for SQLite compatibility. func (r *PlatformAppRepo) SearchByNameByAccount(ctx context.Context, accountID uint, nameQuery string, offset, limit int) ([]model.PlatformApp, int64, error) { var apps []model.PlatformApp var count int64 likeQuery := "%" + nameQuery + "%" db := r.db.WithContext(ctx).Model(&model.PlatformApp{}). Where("account_id = ? AND name LIKE ?", accountID, likeQuery) db.Count(&count) if err := db.Offset(offset).Limit(limit).Order("id ASC").Find(&apps).Error; err != nil { return nil, 0, err } return apps, count, nil } // FindActive retrieves all active PlatformApps (Active=true AND Status=active). func (r *PlatformAppRepo) FindActive(ctx context.Context, offset, limit int) ([]model.PlatformApp, int64, error) { var apps []model.PlatformApp var count int64 db := r.db.WithContext(ctx).Model(&model.PlatformApp{}). Where("active = true AND status = 'active'") db.Count(&count) if err := db.Offset(offset).Limit(limit).Order("id ASC").Find(&apps).Error; err != nil { return nil, 0, err } return apps, count, nil }