package command import ( "errors" "fmt" "time" "github.com/Fimeg/RedFlag/aggregator-server/internal/models" "github.com/google/uuid" ) // Factory creates validated AgentCommand instances type Factory struct { validator *Validator } // NewFactory creates a new command factory func NewFactory() *Factory { return &Factory{ validator: NewValidator(), } } // Create generates a new validated AgentCommand with unique ID func (f *Factory) Create(agentID uuid.UUID, commandType string, params map[string]interface{}) (*models.AgentCommand, error) { cmd := &models.AgentCommand{ ID: uuid.New(), AgentID: agentID, CommandType: commandType, Status: "pending", Source: determineSource(commandType), Params: params, CreatedAt: time.Now(), UpdatedAt: time.Now(), } if err := f.validator.Validate(cmd); err != nil { return nil, fmt.Errorf("command validation failed: %w", err) } return cmd, nil } // determineSource classifies command source based on type func determineSource(commandType string) string { if isSystemCommand(commandType) { return "system" } return "manual" } func isSystemCommand(commandType string) bool { systemCommands := []string{ "enable_heartbeat", "disable_heartbeat", "update_check", "cleanup_old_logs", } for _, cmd := range systemCommands { if commandType == cmd { return true } } return false }