package logs

import (
	
	
	

	
)

// appendJSONIfDebug appends JSON data to a message only if the global log level is Debug
func appendJSONIfDebug( string,  map[string]interface{}) string {
	if internal.LogLevel.DebugOrAbove() {
		,  := json.Marshal()
		return fmt.Sprintf("%s %s", , string())
	}
	return 
}

const (
	// ========================================
	// CIRCUIT_BREAKER.GO - Circuit breaker management
	// ========================================
	CircuitBreakerTransitioningToHalfOpenMessage = "circuit breaker transitioning to half-open"
	CircuitBreakerOpenedMessage                  = "circuit breaker opened"
	CircuitBreakerReopenedMessage                = "circuit breaker reopened"
	CircuitBreakerClosedMessage                  = "circuit breaker closed"
	CircuitBreakerCleanupMessage                 = "circuit breaker cleanup"
	CircuitBreakerOpenMessage                    = "circuit breaker is open, failing fast"

	// ========================================
	// CONFIG.GO - Configuration and debug
	// ========================================
	DebugLoggingEnabledMessage = "debug logging enabled"
	ConfigDebugMessage         = "config debug"

	// ========================================
	// ERRORS.GO - Error message constants
	// ========================================
	InvalidRelaxedTimeoutErrorMessage                 = "relaxed timeout must be greater than 0"
	InvalidHandoffTimeoutErrorMessage                 = "handoff timeout must be greater than 0"
	InvalidHandoffWorkersErrorMessage                 = "MaxWorkers must be greater than or equal to 0"
	InvalidHandoffQueueSizeErrorMessage               = "handoff queue size must be greater than 0"
	InvalidPostHandoffRelaxedDurationErrorMessage     = "post-handoff relaxed duration must be greater than or equal to 0"
	InvalidEndpointTypeErrorMessage                   = "invalid endpoint type"
	InvalidMaintNotificationsErrorMessage             = "invalid maintenance notifications setting (must be 'disabled', 'enabled', or 'auto')"
	InvalidHandoffRetriesErrorMessage                 = "MaxHandoffRetries must be between 1 and 10"
	InvalidClientErrorMessage                         = "invalid client type"
	InvalidNotificationErrorMessage                   = "invalid notification format"
	MaxHandoffRetriesReachedErrorMessage              = "max handoff retries reached"
	HandoffQueueFullErrorMessage                      = "handoff queue is full, cannot queue new handoff requests - consider increasing HandoffQueueSize or MaxWorkers in configuration"
	InvalidCircuitBreakerFailureThresholdErrorMessage = "circuit breaker failure threshold must be >= 1"
	InvalidCircuitBreakerResetTimeoutErrorMessage     = "circuit breaker reset timeout must be >= 0"
	InvalidCircuitBreakerMaxRequestsErrorMessage      = "circuit breaker max requests must be >= 1"
	ConnectionMarkedForHandoffErrorMessage            = "connection marked for handoff"
	ConnectionInvalidHandoffStateErrorMessage         = "connection is in invalid state for handoff"
	ShutdownErrorMessage                              = "shutdown"
	CircuitBreakerOpenErrorMessage                    = "circuit breaker is open, failing fast"

	// ========================================
	// EXAMPLE_HOOKS.GO - Example metrics hooks
	// ========================================
	MetricsHookProcessingNotificationMessage = "metrics hook processing"
	MetricsHookRecordedErrorMessage          = "metrics hook recorded error"

	// ========================================
	// HANDOFF_WORKER.GO - Connection handoff processing
	// ========================================
	HandoffStartedMessage                            = "handoff started"
	HandoffFailedMessage                             = "handoff failed"
	ConnectionNotMarkedForHandoffMessage             = "is not marked for handoff and has no retries"
	ConnectionNotMarkedForHandoffErrorMessage        = "is not marked for handoff"
	HandoffRetryAttemptMessage                       = "Performing handoff"
	CannotQueueHandoffForRetryMessage                = "can't queue handoff for retry"
	HandoffQueueFullMessage                          = "handoff queue is full"
	FailedToDialNewEndpointMessage                   = "failed to dial new endpoint"
	ApplyingRelaxedTimeoutDueToPostHandoffMessage    = "applying relaxed timeout due to post-handoff"
	HandoffSuccessMessage                            = "handoff succeeded"
	RemovingConnectionFromPoolMessage                = "removing connection from pool"
	NoPoolProvidedMessageCannotRemoveMessage         = "no pool provided, cannot remove connection, closing it"
	WorkerExitingDueToShutdownMessage                = "worker exiting due to shutdown"
	WorkerExitingDueToShutdownWhileProcessingMessage = "worker exiting due to shutdown while processing request"
	WorkerPanicRecoveredMessage                      = "worker panic recovered"
	WorkerExitingDueToInactivityTimeoutMessage       = "worker exiting due to inactivity timeout"
	ReachedMaxHandoffRetriesMessage                  = "reached max handoff retries"

	// ========================================
	// MANAGER.GO - Moving operation tracking and handler registration
	// ========================================
	DuplicateMovingOperationMessage  = "duplicate MOVING operation ignored"
	TrackingMovingOperationMessage   = "tracking MOVING operation"
	UntrackingMovingOperationMessage = "untracking MOVING operation"
	OperationNotTrackedMessage       = "operation not tracked"
	FailedToRegisterHandlerMessage   = "failed to register handler"

	// ========================================
	// HOOKS.GO - Notification processing hooks
	// ========================================
	ProcessingNotificationMessage          = "processing notification started"
	ProcessingNotificationFailedMessage    = "processing notification failed"
	ProcessingNotificationSucceededMessage = "processing notification succeeded"

	// ========================================
	// POOL_HOOK.GO - Pool connection management
	// ========================================
	FailedToQueueHandoffMessage = "failed to queue handoff"
	MarkedForHandoffMessage     = "connection marked for handoff"

	// ========================================
	// PUSH_NOTIFICATION_HANDLER.GO - Push notification validation and processing
	// ========================================
	InvalidNotificationFormatMessage              = "invalid notification format"
	InvalidNotificationTypeFormatMessage          = "invalid notification type format"
	InvalidSeqIDInMovingNotificationMessage       = "invalid seqID in MOVING notification"
	InvalidTimeSInMovingNotificationMessage       = "invalid timeS in MOVING notification"
	InvalidNewEndpointInMovingNotificationMessage = "invalid newEndpoint in MOVING notification"
	NoConnectionInHandlerContextMessage           = "no connection in handler context"
	InvalidConnectionTypeInHandlerContextMessage  = "invalid connection type in handler context"
	SchedulingHandoffToCurrentEndpointMessage     = "scheduling handoff to current endpoint"
	RelaxedTimeoutDueToNotificationMessage        = "applying relaxed timeout due to notification"
	UnrelaxedTimeoutMessage                       = "clearing relaxed timeout"
	ManagerNotInitializedMessage                  = "manager not initialized"
	FailedToMarkForHandoffMessage                 = "failed to mark connection for handoff"
	InvalidSeqIDInSMigratingNotificationMessage   = "invalid SeqID in SMIGRATING notification"
	InvalidSeqIDInSMigratedNotificationMessage    = "invalid SeqID in SMIGRATED notification"
	TriggeringClusterStateReloadMessage           = "triggering cluster state reload"

	// ========================================
	// used in pool/conn
	// ========================================
	UnrelaxedTimeoutAfterDeadlineMessage = "clearing relaxed timeout after deadline"
)

func ( uint64,  string) string {
	 := fmt.Sprintf("conn[%d] %s to %s", , HandoffStartedMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":   ,
		"endpoint": ,
	})
}

func ( uint64,  string,  int,  int,  error) string {
	 := fmt.Sprintf("conn[%d] %s to %s (attempt %d/%d): %v", , HandoffFailedMessage, , , , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":      ,
		"endpoint":    ,
		"attempt":     ,
		"maxAttempts": ,
		"error":       .Error(),
	})
}

func ( uint64,  string) string {
	 := fmt.Sprintf("conn[%d] %s to %s", , HandoffSuccessMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":   ,
		"endpoint": ,
	})
}

// Timeout-related log functions
func ( uint64,  string,  interface{}) string {
	 := fmt.Sprintf("conn[%d] %s %s (%v)", , RelaxedTimeoutDueToNotificationMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":           ,
		"notificationType": ,
		"timeout":          fmt.Sprintf("%v", ),
	})
}

func ( uint64) string {
	 := fmt.Sprintf("conn[%d] %s", , UnrelaxedTimeoutMessage)
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
	})
}

func ( uint64) string {
	 := fmt.Sprintf("conn[%d] %s", , UnrelaxedTimeoutAfterDeadlineMessage)
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
	})
}

// Handoff queue and marking functions
func (,  int) string {
	 := fmt.Sprintf("%s (%d/%d), cannot queue new handoff requests - consider increasing HandoffQueueSize or MaxWorkers in configuration", HandoffQueueFullMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"queueLen": ,
		"queueCap": ,
	})
}

func ( uint64,  error) string {
	 := fmt.Sprintf("conn[%d] %s: %v", , FailedToQueueHandoffMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
		"error":  .Error(),
	})
}

func ( uint64,  error) string {
	 := fmt.Sprintf("conn[%d] %s: %v", , FailedToMarkForHandoffMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
		"error":  .Error(),
	})
}

func ( uint64,  string,  error) string {
	 := fmt.Sprintf("conn[%d] %s %s: %v", , FailedToDialNewEndpointMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":   ,
		"endpoint": ,
		"error":    .Error(),
	})
}

func ( uint64,  string,  int) string {
	 := fmt.Sprintf("conn[%d] %s to %s (max retries: %d)", , ReachedMaxHandoffRetriesMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":     ,
		"endpoint":   ,
		"maxRetries": ,
	})
}

// Notification processing functions
func ( uint64,  int64,  string,  interface{}) string {
	 := fmt.Sprintf("conn[%d] seqID[%d] %s %s: %v", , , ProcessingNotificationMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":           ,
		"seqID":            ,
		"notificationType": ,
		"notification":     fmt.Sprintf("%v", ),
	})
}

func ( uint64,  string,  error,  interface{}) string {
	 := fmt.Sprintf("conn[%d] %s %s: %v - %v", , ProcessingNotificationFailedMessage, , , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":           ,
		"notificationType": ,
		"error":            .Error(),
		"notification":     fmt.Sprintf("%v", ),
	})
}

func ( uint64,  string) string {
	 := fmt.Sprintf("conn[%d] %s %s", , ProcessingNotificationSucceededMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":           ,
		"notificationType": ,
	})
}

// Moving operation tracking functions
func ( uint64,  string,  int64) string {
	 := fmt.Sprintf("conn[%d] %s for %s seqID[%d]", , DuplicateMovingOperationMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":   ,
		"endpoint": ,
		"seqID":    ,
	})
}

func ( uint64,  string,  int64) string {
	 := fmt.Sprintf("conn[%d] %s for %s seqID[%d]", , TrackingMovingOperationMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":   ,
		"endpoint": ,
		"seqID":    ,
	})
}

func ( uint64,  int64) string {
	 := fmt.Sprintf("conn[%d] %s seqID[%d]", , UntrackingMovingOperationMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
		"seqID":  ,
	})
}

func ( uint64,  int64) string {
	 := fmt.Sprintf("conn[%d] %s seqID[%d]", , OperationNotTrackedMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
		"seqID":  ,
	})
}

// Connection pool functions
func ( uint64,  error) string {
	 := map[string]interface{}{
		"connID": ,
		"reason": "unknown", // this will be overwritten if reason is not nil
	}
	if  != nil {
		["reason"] = .Error()
	}

	 := fmt.Sprintf("conn[%d] %s due to: %v", , RemovingConnectionFromPoolMessage, )
	return appendJSONIfDebug(, )
}

func ( uint64,  error) string {
	 := map[string]interface{}{
		"connID": ,
		"reason": "unknown", // this will be overwritten if reason is not nil
	}
	if  != nil {
		["reason"] = .Error()
	}

	 := fmt.Sprintf("conn[%d] %s due to: %v", , NoPoolProvidedMessageCannotRemoveMessage, )
	return appendJSONIfDebug(, )
}

// Circuit breaker functions
func ( uint64,  string) string {
	 := fmt.Sprintf("conn[%d] %s for %s", , CircuitBreakerOpenMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":   ,
		"endpoint": ,
	})
}

// Additional handoff functions for specific cases
func ( uint64) string {
	 := fmt.Sprintf("conn[%d] %s", , ConnectionNotMarkedForHandoffMessage)
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
	})
}

func ( uint64) string {
	return fmt.Sprintf("conn[%d] %s", , ConnectionNotMarkedForHandoffErrorMessage)
}

func ( uint64,  int,  string,  string) string {
	 := fmt.Sprintf("conn[%d] Retry %d: %s to %s(was %s)", , , HandoffRetryAttemptMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":      ,
		"retries":     ,
		"newEndpoint": ,
		"oldEndpoint": ,
	})
}

func ( error) string {
	 := fmt.Sprintf("%s: %v", CannotQueueHandoffForRetryMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"error": .Error(),
	})
}

// Validation and error functions
func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", InvalidNotificationFormatMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"notification": fmt.Sprintf("%v", ),
	})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", InvalidNotificationTypeFormatMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"notificationType": fmt.Sprintf("%v", ),
	})
}

// InvalidNotification creates a log message for invalid notifications of any type
func ( string,  interface{}) string {
	 := fmt.Sprintf("invalid %s notification: %v", , )
	return appendJSONIfDebug(, map[string]interface{}{
		"notificationType": ,
		"notification":     fmt.Sprintf("%v", ),
	})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", InvalidSeqIDInMovingNotificationMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"seqID": fmt.Sprintf("%v", ),
	})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", InvalidTimeSInMovingNotificationMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"timeS": fmt.Sprintf("%v", ),
	})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", InvalidNewEndpointInMovingNotificationMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"newEndpoint": fmt.Sprintf("%v", ),
	})
}

func ( string) string {
	 := fmt.Sprintf("%s for %s notification", NoConnectionInHandlerContextMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"notificationType": ,
	})
}

func ( string,  interface{},  interface{}) string {
	 := fmt.Sprintf("%s for %s notification - %T %#v", InvalidConnectionTypeInHandlerContextMessage, , , )
	return appendJSONIfDebug(, map[string]interface{}{
		"notificationType": ,
		"connType":         fmt.Sprintf("%T", ),
	})
}

func ( uint64,  float64) string {
	 := fmt.Sprintf("conn[%d] %s in %v seconds", , SchedulingHandoffToCurrentEndpointMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":  ,
		"seconds": ,
	})
}

func () string {
	return appendJSONIfDebug(ManagerNotInitializedMessage, map[string]interface{}{})
}

func ( string,  error) string {
	 := fmt.Sprintf("%s for %s: %v", FailedToRegisterHandlerMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"notificationType": ,
		"error":            .Error(),
	})
}

func () string {
	return appendJSONIfDebug(ShutdownErrorMessage, map[string]interface{}{})
}

// Configuration validation error functions
func () string {
	return appendJSONIfDebug(InvalidRelaxedTimeoutErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidHandoffTimeoutErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidHandoffWorkersErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidHandoffQueueSizeErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidPostHandoffRelaxedDurationErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidEndpointTypeErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidMaintNotificationsErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidHandoffRetriesErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidClientErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidNotificationErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(MaxHandoffRetriesReachedErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(HandoffQueueFullErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidCircuitBreakerFailureThresholdErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidCircuitBreakerResetTimeoutErrorMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(InvalidCircuitBreakerMaxRequestsErrorMessage, map[string]interface{}{})
}

// Configuration and debug functions
func () string {
	return appendJSONIfDebug(DebugLoggingEnabledMessage, map[string]interface{}{})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s: %+v", ConfigDebugMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"config": fmt.Sprintf("%+v", ),
	})
}

// Handoff worker functions
func () string {
	return appendJSONIfDebug(WorkerExitingDueToShutdownMessage, map[string]interface{}{})
}

func () string {
	return appendJSONIfDebug(WorkerExitingDueToShutdownWhileProcessingMessage, map[string]interface{}{})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", WorkerPanicRecoveredMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"panic": fmt.Sprintf("%v", ),
	})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s (%v)", WorkerExitingDueToInactivityTimeoutMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"timeout": fmt.Sprintf("%v", ),
	})
}

func ( uint64,  interface{},  string) string {
	 := fmt.Sprintf("conn[%d] %s (%v) until %s", , ApplyingRelaxedTimeoutDueToPostHandoffMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"connID":  ,
		"timeout": fmt.Sprintf("%v", ),
		"until":   ,
	})
}

// Example hooks functions
func ( string,  uint64) string {
	 := fmt.Sprintf("%s %s notification on conn[%d]", MetricsHookProcessingNotificationMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"notificationType": ,
		"connID":           ,
	})
}

func ( string,  uint64,  error) string {
	 := fmt.Sprintf("%s for %s notification on conn[%d]: %v", MetricsHookRecordedErrorMessage, , , )
	return appendJSONIfDebug(, map[string]interface{}{
		"notificationType": ,
		"connID":           ,
		"error":            .Error(),
	})
}

// Pool hook functions
func ( uint64) string {
	 := fmt.Sprintf("conn[%d] %s", , MarkedForHandoffMessage)
	return appendJSONIfDebug(, map[string]interface{}{
		"connID": ,
	})
}

// Circuit breaker additional functions
func ( string) string {
	 := fmt.Sprintf("%s for %s", CircuitBreakerTransitioningToHalfOpenMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"endpoint": ,
	})
}

func ( string,  int64) string {
	 := fmt.Sprintf("%s for endpoint %s after %d failures", CircuitBreakerOpenedMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"endpoint": ,
		"failures": ,
	})
}

func ( string) string {
	 := fmt.Sprintf("%s for endpoint %s due to failure in half-open state", CircuitBreakerReopenedMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"endpoint": ,
	})
}

func ( string,  int64) string {
	 := fmt.Sprintf("%s for endpoint %s after %d successful requests", CircuitBreakerClosedMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"endpoint":  ,
		"successes": ,
	})
}

func ( int,  int) string {
	 := fmt.Sprintf("%s removed %d/%d entries", CircuitBreakerCleanupMessage, , )
	return appendJSONIfDebug(, map[string]interface{}{
		"removed": ,
		"total":   ,
	})
}

// ExtractDataFromLogMessage extracts structured data from maintnotifications log messages
// Returns a map containing the parsed key-value pairs from the structured data section
// Example: "conn[123] handoff started to localhost:6379 {"connID":123,"endpoint":"localhost:6379"}"
// Returns: map[string]interface{}{"connID": 123, "endpoint": "localhost:6379"}
func ( string) map[string]interface{} {
	 := make(map[string]interface{})

	// Find the JSON data section at the end of the message
	 := regexp.MustCompile(`(\{.*\})$`)
	 := .FindStringSubmatch()
	if len() < 2 {
		return 
	}

	 := [1]
	if  == "" {
		return 
	}

	// Parse the JSON directly
	var  map[string]interface{}
	if  := json.Unmarshal([]byte(), &);  == nil {
		return 
	}

	// If JSON parsing fails, return empty map
	return 
}

// Cluster notification functions
func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", InvalidSeqIDInSMigratingNotificationMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"seqID": fmt.Sprintf("%v", ),
	})
}

func ( interface{}) string {
	 := fmt.Sprintf("%s: %v", InvalidSeqIDInSMigratedNotificationMessage, )
	return appendJSONIfDebug(, map[string]interface{}{
		"seqID": fmt.Sprintf("%v", ),
	})
}

// TriggeringClusterStateReload logs when cluster state reload is triggered (deduplicated, once per seqID)
func ( int64,  string,  []string) string {
	 := fmt.Sprintf("%s seqID=%d host:port=%s slots=%v", TriggeringClusterStateReloadMessage, , , )
	return appendJSONIfDebug(, map[string]interface{}{
		"seqID":      ,
		"hostPort":   ,
		"slotRanges": ,
	})
}