package service import ( "context" "fmt" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" applogger "github.com/gochat/gochat/pkg/logger" ) // AgentBotInboxService implements business logic for bot-inbox bindings. // Reference: Chatwoot app/controllers/api/v1/accounts/agent_bot_inboxes_controller.rb type AgentBotInboxService struct { repo *repository.AgentBotInboxRepo botRepo *repository.AgentBotRepo } // NewAgentBotInboxService creates a new AgentBotInbox service. func NewAgentBotInboxService(repo *repository.AgentBotInboxRepo, botRepo *repository.AgentBotRepo) *AgentBotInboxService { return &AgentBotInboxService{repo: repo, botRepo: botRepo} } // BindBotToInboxRequest is the DTO for binding a bot to an inbox. type BindBotToInboxRequest struct { AgentBotID uint `json:"agent_bot_id" validate:"required"` InboxID uint `json:"inbox_id" validate:"required"` } // UpdateBindingStatusRequest is the DTO for toggling a binding's active/inactive status. type UpdateBindingStatusRequest struct { Status int `json:"status" validate:"required,oneof=0 1"` } // Bind creates a new bot-inbox binding with status=active. // accountID is auto-filled from the inbox's account_id. func (s *AgentBotInboxService) Bind(ctx context.Context, accountID uint, req BindBotToInboxRequest) (*model.AgentBotInbox, error) { // Verify the bot exists bot, err := s.botRepo.FindByID(ctx, req.AgentBotID) if err != nil { applogger.L().Errorf("Bind bot to inbox: bot %d not found: %v", req.AgentBotID, err) return nil, fmt.Errorf("agent bot not found: %w", err) } // Check for duplicate binding existing, err := s.repo.FindByAgentBotAndInbox(ctx, req.AgentBotID, req.InboxID) if err == nil && existing != nil { applogger.L().Errorf("Bind bot to inbox: duplicate binding bot=%d inbox=%d", req.AgentBotID, req.InboxID) return nil, fmt.Errorf("bot already bound to this inbox") } abi := &model.AgentBotInbox{ AgentBotID: req.AgentBotID, InboxID: req.InboxID, AccountID: bot.AccountID, // auto-fill from bot's account; may be nil for platform bots Status: model.AgentBotInboxActive, } if err := s.repo.Create(ctx, abi); err != nil { applogger.L().Errorf("Bind bot to inbox: create failed: %v", err) return nil, err } applogger.L().Infof("Bound agent bot %d to inbox %d (binding %d)", req.AgentBotID, req.InboxID, abi.ID) return abi, nil } // Unbind removes a bot-inbox binding by ID. func (s *AgentBotInboxService) Unbind(ctx context.Context, id uint) error { if err := s.repo.Delete(ctx, id); err != nil { applogger.L().Errorf("Unbind bot-inbox binding %d: %v", id, err) return err } applogger.L().Infof("Unbound bot-inbox binding %d", id) return nil } // UpdateStatus toggles the active/inactive status of a bot-inbox binding. func (s *AgentBotInboxService) UpdateStatus(ctx context.Context, id uint, req UpdateBindingStatusRequest) (*model.AgentBotInbox, error) { abi, err := s.repo.FindByID(ctx, id) if err != nil { applogger.L().Errorf("Update binding status %d: not found: %v", id, err) return nil, fmt.Errorf("binding not found: %w", err) } abi.Status = model.AgentBotInboxStatus(req.Status) if err := s.repo.Update(ctx, abi); err != nil { applogger.L().Errorf("Update binding status %d: save failed: %v", id, err) return nil, err } applogger.L().Infof("Updated bot-inbox binding %d status to %d", id, req.Status) return abi, nil } // ListByInbox retrieves all bot-inbox bindings for a given inbox. func (s *AgentBotInboxService) ListByInbox(ctx context.Context, inboxID uint) ([]model.AgentBotInbox, error) { abis, err := s.repo.FindByInboxID(ctx, inboxID) if err != nil { applogger.L().Errorf("List bot-inbox bindings for inbox %d: %v", inboxID, err) return nil, err } return abis, nil } // ListActiveByInbox retrieves only active bot-inbox bindings for a given inbox. func (s *AgentBotInboxService) ListActiveByInbox(ctx context.Context, inboxID uint) ([]model.AgentBotInbox, error) { abis, err := s.repo.FindActiveByInboxID(ctx, inboxID) if err != nil { applogger.L().Errorf("List active bot-inbox bindings for inbox %d: %v", inboxID, err) return nil, err } return abis, nil } // ListByBot retrieves all bot-inbox bindings for a given agent bot. func (s *AgentBotInboxService) ListByBot(ctx context.Context, agentBotID uint) ([]model.AgentBotInbox, error) { abis, err := s.repo.FindByAgentBotID(ctx, agentBotID) if err != nil { applogger.L().Errorf("List bot-inbox bindings for bot %d: %v", agentBotID, err) return nil, err } return abis, nil }