package redis

import (
	
	
	
	
	
	

	
	
)

var (
	errInvalidCmdPointer         = errors.New("redis: invalid command pointer")
	errNoCmdsToAggregate         = errors.New("redis: no commands to aggregate")
	errNoResToAggregate          = errors.New("redis: no results to aggregate")
	errInvalidCursorCmdArgsCount = errors.New("redis: FT.CURSOR command requires at least 3 arguments")
	errInvalidCursorIdType       = errors.New("redis: invalid cursor ID type")
)

// slotResult represents the result of executing a command on a specific slot
type slotResult struct {
	cmd  Cmder
	keys []string
	err  error
}

// routeAndRun routes a command to the appropriate cluster nodes and executes it
func ( *ClusterClient) ( context.Context,  Cmder,  *clusterNode) error {
	var  *routing.CommandPolicy
	if .cmdInfoResolver != nil {
		 = .cmdInfoResolver.GetCommandPolicy(, )
	}

	// Set stepCount from cmdInfo if not already set
	if .stepCount() == 0 {
		if  := .cmdInfo(, .Name());  != nil && .StepCount > 0 {
			.SetStepCount(.StepCount)
		}
	}

	if  == nil {
		return .executeDefault(, , , )
	}
	switch .Request {
	case routing.ReqAllNodes:
		return .executeOnAllNodes(, , )
	case routing.ReqAllShards:
		return .executeOnAllShards(, , )
	case routing.ReqMultiShard:
		return .executeMultiShard(, , )
	case routing.ReqSpecial:
		return .executeSpecialCommand(, , , )
	default:
		return .executeDefault(, , , )
	}
}

// executeDefault handles standard command routing based on keys
func ( *ClusterClient) ( context.Context,  Cmder,  *routing.CommandPolicy,  *clusterNode) error {
	if  != nil && !.hasKeys() {
		if .readOnlyEnabled() && .IsReadOnly() {
			return .executeOnArbitraryNode(, )
		}
	}

	return .Client.Process(, )
}

// executeOnArbitraryNode routes command to an arbitrary node
func ( *ClusterClient) ( context.Context,  Cmder) error {
	 := .pickArbitraryNode()
	if  == nil {
		return errClusterNoNodes
	}
	return .Client.Process(, )
}

// executeOnAllNodes executes command on all nodes (masters and replicas)
func ( *ClusterClient) ( context.Context,  Cmder,  *routing.CommandPolicy) error {
	,  := .state.Get()
	if  != nil {
		return 
	}

	 := make([]*clusterNode, 0, len(.Masters)+len(.Slaves))
	 = append(, .Masters...)
	 = append(, .Slaves...)
	if len() == 0 {
		return errClusterNoNodes
	}

	return .executeParallel(, , , )
}

// executeOnAllShards executes command on all master shards
func ( *ClusterClient) ( context.Context,  Cmder,  *routing.CommandPolicy) error {
	,  := .state.Get()
	if  != nil {
		return 
	}

	if len(.Masters) == 0 {
		return errClusterNoNodes
	}

	return .executeParallel(, , .Masters, )
}

// executeMultiShard handles commands that operate on multiple keys across shards
func ( *ClusterClient) ( context.Context,  Cmder,  *routing.CommandPolicy) error {
	 := .Args()
	 := cmdFirstKeyPosWithInfo(, .cmdInfoPeek(.Name()))
	 := int(.stepCount())
	if  == 0 {
		 = 1 // Default to 1 if not set
	}

	if  == 0 ||  >= len() {
		return fmt.Errorf("redis: multi-shard command %s has no key arguments", .Name())
	}

	// Group keys by slot
	 := make(map[int][]string)
	 := make([]string, 0)

	for  := ;  < len();  +=  {
		,  := [].(string)
		if ! {
			return fmt.Errorf("redis: non-string key at position %d: %v", , [])
		}

		 := hashtag.Slot()
		[] = append([], )
		for  := 1;  < ; ++ {
			if + >= len() {
				break
			}
			[] = append([], [+].(string))
		}
		 = append(, )
	}

	return .executeMultiSlot(, , , , , )
}

// executeMultiSlot executes commands across multiple slots concurrently
func ( *ClusterClient) ( context.Context,  Cmder,  map[int][]string,  []string,  *routing.CommandPolicy,  int) error {
	 := make(chan slotResult, len())
	var  sync.WaitGroup

	// Execute on each slot concurrently
	for ,  := range  {
		.Add(1)
		go func( int,  []string) {
			defer .Done()

			,  := .cmdNodeWithShardPicker(, .Name(), , .opt.ShardPicker)
			if  != nil {
				 <- slotResult{nil, , }
				return
			}

			// Create a command for this specific slot's keys
			 := .createSlotSpecificCommand(, , , )
			 = .Client.Process(, )
			 <- slotResult{, , }
		}(, )
	}

	go func() {
		.Wait()
		close()
	}()

	return .aggregateMultiSlotResults(, , , , )
}

// createSlotSpecificCommand creates a new command for a specific slot's keys.
// firstKeyPos is passed in from the caller (computed once in executeMultiShard)
// so this function never independently re-peeks the cache — avoids the
// cold --> warm inconsistency the reviewer flagged.
func ( *ClusterClient) ( context.Context,  Cmder,  []string,  int) Cmder {
	 := .Args()

	// Build new args with only the specified keys
	 := make([]interface{}, 0, +len())

	// Copy command name and arguments before the keys
	 = append(, [:]...)

	// Add the slot-specific keys
	for ,  := range  {
		 = append(, )
	}

	// Create a new command of the same type using the helper function
	return createCommandByType(, .GetCmdType(), ...)
}

// createCommandByType creates a new command of the specified type with the given arguments
func createCommandByType( context.Context,  CmdType,  ...interface{}) Cmder {
	switch  {
	case CmdTypeString:
		return NewStringCmd(, ...)
	case CmdTypeInt:
		return NewIntCmd(, ...)
	case CmdTypeBool:
		return NewBoolCmd(, ...)
	case CmdTypeFloat:
		return NewFloatCmd(, ...)
	case CmdTypeStringSlice:
		return NewStringSliceCmd(, ...)
	case CmdTypeIntSlice:
		return NewIntSliceCmd(, ...)
	case CmdTypeFloatSlice:
		return NewFloatSliceCmd(, ...)
	case CmdTypeBoolSlice:
		return NewBoolSliceCmd(, ...)
	case CmdTypeStatus:
		return NewStatusCmd(, ...)
	case CmdTypeTime:
		return NewTimeCmd(, ...)
	case CmdTypeMapStringString:
		return NewMapStringStringCmd(, ...)
	case CmdTypeMapStringInt:
		return NewMapStringIntCmd(, ...)
	case CmdTypeMapStringInterface:
		return NewMapStringInterfaceCmd(, ...)
	case CmdTypeMapStringInterfaceSlice:
		return NewMapStringInterfaceSliceCmd(, ...)
	case CmdTypeSlice:
		return NewSliceCmd(, ...)
	case CmdTypeStringStructMap:
		return NewStringStructMapCmd(, ...)
	case CmdTypeXMessageSlice:
		return NewXMessageSliceCmd(, ...)
	case CmdTypeXStreamSlice:
		return NewXStreamSliceCmd(, ...)
	case CmdTypeXPending:
		return NewXPendingCmd(, ...)
	case CmdTypeXPendingExt:
		return NewXPendingExtCmd(, ...)
	case CmdTypeXAutoClaim:
		return NewXAutoClaimCmd(, ...)
	case CmdTypeXAutoClaimWithDeleted:
		return NewXAutoClaimWithDeletedCmd(, ...)
	case CmdTypeXAutoClaimJustID:
		return NewXAutoClaimJustIDCmd(, ...)
	case CmdTypeXInfoStreamFull:
		return NewXInfoStreamFullCmd(, ...)
	case CmdTypeZSlice:
		return NewZSliceCmd(, ...)
	case CmdTypeZWithKey:
		return NewZWithKeyCmd(, ...)
	case CmdTypeClusterSlots:
		return NewClusterSlotsCmd(, ...)
	case CmdTypeGeoPos:
		return NewGeoPosCmd(, ...)
	case CmdTypeCommandsInfo:
		return NewCommandsInfoCmd(, ...)
	case CmdTypeSlowLog:
		return NewSlowLogCmd(, ...)
	case CmdTypeKeyValues:
		return NewKeyValuesCmd(, ...)
	case CmdTypeZSliceWithKey:
		return NewZSliceWithKeyCmd(, ...)
	case CmdTypeFunctionList:
		return NewFunctionListCmd(, ...)
	case CmdTypeFunctionStats:
		return NewFunctionStatsCmd(, ...)
	case CmdTypeKeyFlags:
		return NewKeyFlagsCmd(, ...)
	case CmdTypeDuration:
		return NewDurationCmd(, time.Millisecond, ...)
	}
	return NewCmd(, ...)
}

// executeSpecialCommand handles commands with special routing requirements
func ( *ClusterClient) ( context.Context,  Cmder,  *routing.CommandPolicy,  *clusterNode) error {
	switch .Name() {
	case "ft.cursor":
		return .executeCursorCommand(, )
	default:
		return .executeDefault(, , , )
	}
}

// executeCursorCommand handles FT.CURSOR commands with sticky routing
func ( *ClusterClient) ( context.Context,  Cmder) error {
	 := .Args()
	if len() < 4 {
		return errInvalidCursorCmdArgsCount
	}

	,  := [3].(string)
	if ! {
		return errInvalidCursorIdType
	}

	// Route based on cursor ID to maintain stickiness
	 := hashtag.Slot()
	,  := .cmdNodeWithShardPicker(, .Name(), , .opt.ShardPicker)
	if  != nil {
		return 
	}

	return .Client.Process(, )
}

// executeParallel executes a command on multiple nodes concurrently
func ( *ClusterClient) ( context.Context,  Cmder,  []*clusterNode,  *routing.CommandPolicy) error {
	if len() == 0 {
		return errClusterNoNodes
	}

	if len() == 1 {
		return [0].Client.Process(, )
	}

	type  struct {
		 Cmder
		 error
	}

	 := make(chan , len())
	var  sync.WaitGroup

	for ,  := range  {
		.Add(1)
		go func( *clusterNode) {
			defer .Done()
			 := .Clone()
			 := .Client.Process(, )
			 <- {, }
		}()
	}

	go func() {
		.Wait()
		close()
	}()

	// Collect results and check for errors
	 := make([]Cmder, 0, len())
	var  error

	for  := range  {
		if . != nil &&  == nil {
			 = .
		}
		 = append(, .)
	}

	// If there was an error and no policy specified, fail fast
	if  != nil && ( == nil || .Response == routing.RespDefaultKeyless) {
		.SetErr()
		return 
	}

	return .aggregateResponses(, , )
}

// aggregateMultiSlotResults aggregates results from multi-slot execution
func ( *ClusterClient) ( context.Context,  Cmder,  <-chan slotResult,  []string,  *routing.CommandPolicy) error {
	 := make(map[string]routing.AggregatorResErr)
	var  error

	for  := range  {
		if .err != nil &&  == nil {
			 = .err
		}
		if .cmd != nil && .err == nil {
			,  := ExtractCommandValue(.cmd)

			// Check if the result is a slice (e.g., from MGET)
			if ,  := .([]interface{});  {
				// Map each element to its corresponding key
				for ,  := range .keys {
					if  < len() {
						[] = routing.AggregatorResErr{Result: [], Err: }
					} else {
						[] = routing.AggregatorResErr{Result: nil, Err: }
					}
				}
			} else {
				// For non-slice results, map the entire result to each key
				for ,  := range .keys {
					[] = routing.AggregatorResErr{Result: , Err: }
				}
			}
		}

		// TODO: return multiple errors by order when we will implement multiple errors returning
		if .err != nil {
			 = .err
		}
	}

	return .aggregateKeyedValues(, , , )
}

// aggregateKeyedValues aggregates individual key-value pairs while preserving key order
func ( *ClusterClient) ( Cmder,  map[string]routing.AggregatorResErr,  []string,  *routing.CommandPolicy) error {
	if len() == 0 {
		return errNoResToAggregate
	}

	 := .createAggregator(, , true)

	// Set key order for keyed aggregators
	var  *routing.DefaultKeyedAggregator
	var  bool
	var  error
	if ,  = .(*routing.DefaultKeyedAggregator);  {
		 = .BatchAddWithKeyOrder(, )
	} else {
		 = .BatchAdd()
	}

	if  != nil {
		return 
	}

	return .finishAggregation(, )
}

// aggregateResponses aggregates multiple shard responses
func ( *ClusterClient) ( Cmder,  []Cmder,  *routing.CommandPolicy) error {
	if len() == 0 {
		return errNoCmdsToAggregate
	}

	if len() == 1 {
		 := [0]
		if  := .Err();  != nil {
			.SetErr()
			return 
		}
		,  := ExtractCommandValue()
		return .setCommandValue(, )
	}

	 := .createAggregator(, , false)

	 := []routing.AggregatorResErr{}
	// Add all results to aggregator
	for ,  := range  {
		,  := ExtractCommandValue()
		 = append(, routing.AggregatorResErr{
			Result: ,
			Err:    ,
		})
	}

	 := .BatchSlice()
	if  != nil {
		return 
	}

	return .finishAggregation(, )
}

// createAggregator creates the appropriate response aggregator
func ( *ClusterClient) ( *routing.CommandPolicy,  Cmder,  bool) routing.ResponseAggregator {
	if  != nil {
		return routing.NewResponseAggregator(.Response, .Name())
	}

	if ! {
		 := cmdFirstKeyPosWithInfo(, .cmdInfoPeek(.Name()))
		 =  > 0
	}

	return routing.NewDefaultAggregator()
}

// finishAggregation completes the aggregation process and sets the result
func ( *ClusterClient) ( Cmder,  routing.ResponseAggregator) error {
	,  := .Result()
	if  != nil {
		.SetErr()
		return 
	}

	return .setCommandValue(, )
}

// pickArbitraryNode selects a master or slave shard using the configured ShardPicker
func ( *ClusterClient) ( context.Context) *clusterNode {
	,  := .state.Get()
	if  != nil || len(.Masters) == 0 {
		return nil
	}

	// Index into masters+slaves without materializing a combined slice.
	// append(state.Masters, state.Slaves...) writes into the shared snapshot's
	// spare capacity and races other routers, so pick directly.
	 := .opt.ShardPicker.Next(len(.Masters) + len(.Slaves))
	if  < len(.Masters) {
		return .Masters[]
	}
	return .Slaves[-len(.Masters)]
}

// hasKeys checks if a command operates on keys
func ( *ClusterClient) ( Cmder) bool {
	 := cmdFirstKeyPosWithInfo(, .cmdInfoPeek(.Name()))
	return  > 0
}

func ( *ClusterClient) () bool {
	return .opt.ReadOnly
}

// setCommandValue sets the aggregated value on a command using the enum-based approach
func ( *ClusterClient) ( Cmder,  interface{}) error {
	// If value is nil, it might mean ExtractCommandValue couldn't extract the value
	// but the command might have executed successfully. In this case, don't set an error.
	if  == nil {
		// ExtractCommandValue returned nil - this means the command type is not supported
		// in the aggregation flow. This is a programming error, not a runtime error.
		if .Err() != nil {
			// Command already has an error, preserve it
			return .Err()
		}
		// Command executed successfully but we can't extract/set the aggregated value
		// This indicates the command type needs to be added to ExtractCommandValue
		return fmt.Errorf("redis: cannot aggregate command %s: unsupported command type %d",
			.Name(), .GetCmdType())
	}

	switch .GetCmdType() {
	case CmdTypeGeneric:
		if ,  := .(*Cmd);  {
			.SetVal()
		}
	case CmdTypeString:
		if ,  := .(*StringCmd);  {
			if ,  := .(string);  {
				.SetVal()
			}
		}
	case CmdTypeInt:
		if ,  := .(*IntCmd);  {
			if ,  := .(int64);  {
				.SetVal()
			} else if ,  := .(float64);  {
				.SetVal(int64())
			}
		}
	case CmdTypeBool:
		if ,  := .(*BoolCmd);  {
			if ,  := .(bool);  {
				.SetVal()
			}
		}
	case CmdTypeFloat:
		if ,  := .(*FloatCmd);  {
			if ,  := .(float64);  {
				.SetVal()
			}
		}
	case CmdTypeStringSlice:
		if ,  := .(*StringSliceCmd);  {
			if ,  := .([]string);  {
				.SetVal()
			}
		}
	case CmdTypeIntSlice:
		if ,  := .(*IntSliceCmd);  {
			if ,  := .([]int64);  {
				.SetVal()
			} else if ,  := .([]float64);  {
				 := len()
				 := make([]int, )
				for  := range  {
					[] = int([])
				}
			}
		}
	case CmdTypeFloatSlice:
		if ,  := .(*FloatSliceCmd);  {
			if ,  := .([]float64);  {
				.SetVal()
			}
		}
	case CmdTypeBoolSlice:
		if ,  := .(*BoolSliceCmd);  {
			if ,  := .([]bool);  {
				.SetVal()
			}
		}
	case CmdTypeMapStringString:
		if ,  := .(*MapStringStringCmd);  {
			if ,  := .(map[string]string);  {
				.SetVal()
			}
		}
	case CmdTypeMapStringInt:
		if ,  := .(*MapStringIntCmd);  {
			if ,  := .(map[string]int64);  {
				.SetVal()
			}
		}
	case CmdTypeMapStringInterface:
		if ,  := .(*MapStringInterfaceCmd);  {
			if ,  := .(map[string]interface{});  {
				.SetVal()
			}
		}
	case CmdTypeSlice:
		if ,  := .(*SliceCmd);  {
			if ,  := .([]interface{});  {
				.SetVal()
			}
		}
	case CmdTypeStatus:
		if ,  := .(*StatusCmd);  {
			if ,  := .(string);  {
				.SetVal()
			}
		}
	case CmdTypeDuration:
		if ,  := .(*DurationCmd);  {
			if ,  := .(time.Duration);  {
				.SetVal()
			}
		}
	case CmdTypeTime:
		if ,  := .(*TimeCmd);  {
			if ,  := .(time.Time);  {
				.SetVal()
			}
		}
	case CmdTypeKeyValueSlice:
		if ,  := .(*KeyValueSliceCmd);  {
			if ,  := .([]KeyValue);  {
				.SetVal()
			}
		}
	case CmdTypeStringStructMap:
		if ,  := .(*StringStructMapCmd);  {
			if ,  := .(map[string]struct{});  {
				.SetVal()
			}
		}
	case CmdTypeXMessageSlice:
		if ,  := .(*XMessageSliceCmd);  {
			if ,  := .([]XMessage);  {
				.SetVal()
			}
		}
	case CmdTypeXStreamSlice:
		if ,  := .(*XStreamSliceCmd);  {
			if ,  := .([]XStream);  {
				.SetVal()
			}
		}
	case CmdTypeXPending:
		if ,  := .(*XPendingCmd);  {
			if ,  := .(*XPending);  {
				.SetVal()
			}
		}
	case CmdTypeXPendingExt:
		if ,  := .(*XPendingExtCmd);  {
			if ,  := .([]XPendingExt);  {
				.SetVal()
			}
		}
	case CmdTypeXAutoClaim:
		if ,  := .(*XAutoClaimCmd);  {
			if ,  := .(CmdTypeXAutoClaimValue);  {
				.SetVal(.messages, .start)
			}
		}
	case CmdTypeXAutoClaimWithDeleted:
		if ,  := .(*XAutoClaimWithDeletedCmd);  {
			if ,  := .(CmdTypeXAutoClaimWithDeletedValue);  {
				.SetVal(.messages, .start, .deletedIDs)
			}
		}
	case CmdTypeXAutoClaimJustID:
		if ,  := .(*XAutoClaimJustIDCmd);  {
			if ,  := .(CmdTypeXAutoClaimJustIDValue);  {
				.SetVal(.ids, .start)
			}
		}
	case CmdTypeXInfoConsumers:
		if ,  := .(*XInfoConsumersCmd);  {
			if ,  := .([]XInfoConsumer);  {
				.SetVal()
			}
		}
	case CmdTypeXInfoGroups:
		if ,  := .(*XInfoGroupsCmd);  {
			if ,  := .([]XInfoGroup);  {
				.SetVal()
			}
		}
	case CmdTypeXInfoStream:
		if ,  := .(*XInfoStreamCmd);  {
			if ,  := .(*XInfoStream);  {
				.SetVal()
			}
		}
	case CmdTypeXInfoStreamFull:
		if ,  := .(*XInfoStreamFullCmd);  {
			if ,  := .(*XInfoStreamFull);  {
				.SetVal()
			}
		}
	case CmdTypeZSlice:
		if ,  := .(*ZSliceCmd);  {
			if ,  := .([]Z);  {
				.SetVal()
			}
		}
	case CmdTypeZWithKey:
		if ,  := .(*ZWithKeyCmd);  {
			if ,  := .(*ZWithKey);  {
				.SetVal()
			}
		}
	case CmdTypeScan:
		if ,  := .(*ScanCmd);  {
			if ,  := .(CmdTypeScanValue);  {
				.SetVal(.keys, .cursor)
			}
		}
	case CmdTypeClusterSlots:
		if ,  := .(*ClusterSlotsCmd);  {
			if ,  := .([]ClusterSlot);  {
				.SetVal()
			}
		}
	case CmdTypeGeoLocation:
		if ,  := .(*GeoLocationCmd);  {
			if ,  := .([]GeoLocation);  {
				.SetVal()
			}
		}
	case CmdTypeGeoSearchLocation:
		if ,  := .(*GeoSearchLocationCmd);  {
			if ,  := .([]GeoLocation);  {
				.SetVal()
			}
		}
	case CmdTypeGeoPos:
		if ,  := .(*GeoPosCmd);  {
			if ,  := .([]*GeoPos);  {
				.SetVal()
			}
		}
	case CmdTypeCommandsInfo:
		if ,  := .(*CommandsInfoCmd);  {
			if ,  := .(map[string]*CommandInfo);  {
				.SetVal()
			}
		}
	case CmdTypeSlowLog:
		if ,  := .(*SlowLogCmd);  {
			if ,  := .([]SlowLog);  {
				.SetVal()
			}
		}
	case CmdTypeMapStringStringSlice:
		if ,  := .(*MapStringStringSliceCmd);  {
			if ,  := .([]map[string]string);  {
				.SetVal()
			}
		}
	case CmdTypeMapMapStringInterface:
		if ,  := .(*MapMapStringInterfaceCmd);  {
			if ,  := .(map[string]interface{});  {
				.SetVal()
			}
		}
	case CmdTypeMapStringInterfaceSlice:
		if ,  := .(*MapStringInterfaceSliceCmd);  {
			if ,  := .([]map[string]interface{});  {
				.SetVal()
			}
		}
	case CmdTypeKeyValues:
		if ,  := .(*KeyValuesCmd);  {
			// KeyValuesCmd needs a key string and values slice
			if ,  := .(CmdTypeKeyValuesValue);  {
				.SetVal(.key, .values)
			}
		}
	case CmdTypeZSliceWithKey:
		if ,  := .(*ZSliceWithKeyCmd);  {
			// ZSliceWithKeyCmd needs a key string and Z slice
			if ,  := .(CmdTypeZSliceWithKeyValue);  {
				.SetVal(.key, .zSlice)
			}
		}
	case CmdTypeFunctionList:
		if ,  := .(*FunctionListCmd);  {
			if ,  := .([]Library);  {
				.SetVal()
			}
		}
	case CmdTypeFunctionStats:
		if ,  := .(*FunctionStatsCmd);  {
			if ,  := .(FunctionStats);  {
				.SetVal()
			}
		}
	case CmdTypeLCS:
		if ,  := .(*LCSCmd);  {
			if ,  := .(*LCSMatch);  {
				.SetVal()
			}
		}
	case CmdTypeKeyFlags:
		if ,  := .(*KeyFlagsCmd);  {
			if ,  := .([]KeyFlags);  {
				.SetVal()
			}
		}
	case CmdTypeClusterLinks:
		if ,  := .(*ClusterLinksCmd);  {
			if ,  := .([]ClusterLink);  {
				.SetVal()
			}
		}
	case CmdTypeClusterShards:
		if ,  := .(*ClusterShardsCmd);  {
			if ,  := .([]ClusterShard);  {
				.SetVal()
			}
		}
	case CmdTypeRankWithScore:
		if ,  := .(*RankWithScoreCmd);  {
			if ,  := .(RankScore);  {
				.SetVal()
			}
		}
	case CmdTypeClientInfo:
		if ,  := .(*ClientInfoCmd);  {
			if ,  := .(*ClientInfo);  {
				.SetVal()
			}
		}
	case CmdTypeACLLog:
		if ,  := .(*ACLLogCmd);  {
			if ,  := .([]*ACLLogEntry);  {
				.SetVal()
			}
		}
	case CmdTypeInfo:
		if ,  := .(*InfoCmd);  {
			if ,  := .(map[string]map[string]string);  {
				.SetVal()
			}
		}
	case CmdTypeMonitor:
		// MonitorCmd doesn't have SetVal method
		// Skip setting value for MonitorCmd
	case CmdTypeJSON:
		if ,  := .(*JSONCmd);  {
			if ,  := .(string);  {
				.SetVal()
			}
		}
	case CmdTypeJSONSlice:
		if ,  := .(*JSONSliceCmd);  {
			if ,  := .([]interface{});  {
				.SetVal()
			}
		}
	case CmdTypeIntPointerSlice:
		if ,  := .(*IntPointerSliceCmd);  {
			if ,  := .([]*int64);  {
				.SetVal()
			}
		}
	case CmdTypeScanDump:
		if ,  := .(*ScanDumpCmd);  {
			if ,  := .(ScanDump);  {
				.SetVal()
			}
		}
	case CmdTypeBFInfo:
		if ,  := .(*BFInfoCmd);  {
			if ,  := .(BFInfo);  {
				.SetVal()
			}
		}
	case CmdTypeCFInfo:
		if ,  := .(*CFInfoCmd);  {
			if ,  := .(CFInfo);  {
				.SetVal()
			}
		}
	case CmdTypeCMSInfo:
		if ,  := .(*CMSInfoCmd);  {
			if ,  := .(CMSInfo);  {
				.SetVal()
			}
		}
	case CmdTypeTopKInfo:
		if ,  := .(*TopKInfoCmd);  {
			if ,  := .(TopKInfo);  {
				.SetVal()
			}
		}
	case CmdTypeTDigestInfo:
		if ,  := .(*TDigestInfoCmd);  {
			if ,  := .(TDigestInfo);  {
				.SetVal()
			}
		}
	case CmdTypeFTSynDump:
		if ,  := .(*FTSynDumpCmd);  {
			if ,  := .([]FTSynDumpResult);  {
				.SetVal()
			}
		}
	case CmdTypeAggregate:
		if ,  := .(*AggregateCmd);  {
			if ,  := .(*FTAggregateResult);  {
				.SetVal()
			}
		}
	case CmdTypeFTInfo:
		if ,  := .(*FTInfoCmd);  {
			if ,  := .(FTInfoResult);  {
				.SetVal()
			}
		}
	case CmdTypeFTSpellCheck:
		if ,  := .(*FTSpellCheckCmd);  {
			if ,  := .([]SpellCheckResult);  {
				.SetVal()
			}
		}
	case CmdTypeFTSearch:
		if ,  := .(*FTSearchCmd);  {
			if ,  := .(FTSearchResult);  {
				.SetVal()
			}
		}
	case CmdTypeTSTimestampValue:
		if ,  := .(*TSTimestampValueCmd);  {
			if ,  := .(TSTimestampValue);  {
				.SetVal()
			}
		}
	case CmdTypeTSTimestampValueSlice:
		if ,  := .(*TSTimestampValueSliceCmd);  {
			if ,  := .([]TSTimestampValue);  {
				.SetVal()
			}
		}
	default:
		// Fallback to reflection for unknown types
		return .setCommandValueReflection(, )
	}

	return nil
}

// setCommandValueReflection is a fallback function that uses reflection
func ( *ClusterClient) ( Cmder,  interface{}) error {
	 := reflect.ValueOf()
	if .Kind() != reflect.Pointer || .IsNil() {
		return errInvalidCmdPointer
	}

	 := .MethodByName("SetVal")
	if !.IsValid() {
		return fmt.Errorf("redis: command %T does not have SetVal method", )
	}

	 := []reflect.Value{reflect.ValueOf()}

	switch .(type) {
	case *XAutoClaimCmd, *XAutoClaimJustIDCmd:
		 = append(, reflect.ValueOf(""))
	case *ScanCmd:
		 = append(, reflect.ValueOf(uint64(0)))
	case *KeyValuesCmd, *ZSliceWithKeyCmd:
		if ,  := .(string);  {
			 = []reflect.Value{reflect.ValueOf()}
			if ,  := .(*ZSliceWithKeyCmd);  {
				 = append(, reflect.ValueOf([]Z{}))
			} else {
				 = append(, reflect.ValueOf([]string{}))
			}
		}
	}

	defer func() {
		if  := recover();  != nil {
			.SetErr(fmt.Errorf("redis: failed to set command value: %v", ))
		}
	}()

	.Call()
	return nil
}