package maintnotifications

import (
	
	
	

	
	
	
	
)

// contextKey is a custom type for context keys to avoid collisions
type contextKey string

const (
	startTimeKey contextKey = "maint_notif_start_time"
)

// MetricsHook collects metrics about notification processing.
type MetricsHook struct {
	NotificationCounts map[string]int64
	ProcessingTimes    map[string]time.Duration
	ErrorCounts        map[string]int64
	HandoffCounts      int64 // Total handoffs initiated
	HandoffSuccesses   int64 // Successful handoffs
	HandoffFailures    int64 // Failed handoffs
}

// NewMetricsHook creates a new metrics collection hook.
func () *MetricsHook {
	return &MetricsHook{
		NotificationCounts: make(map[string]int64),
		ProcessingTimes:    make(map[string]time.Duration),
		ErrorCounts:        make(map[string]int64),
	}
}

// PreHook records the start time for processing metrics.
func ( *MetricsHook) ( context.Context,  push.NotificationHandlerContext,  string,  []interface{}) ([]interface{}, bool) {
	.NotificationCounts[]++

	// Log connection information if available
	if ,  := .Conn.(*pool.Conn);  {
		internal.Logger.Printf(, "%s", logs.MetricsHookProcessingNotification(, .GetID()))
	}

	// Store start time in context for duration calculation
	 := time.Now()
	_ = context.WithValue(, startTimeKey, ) // Context not used further

	return , true
}

// PostHook records processing completion and any errors.
func ( *MetricsHook) ( context.Context,  push.NotificationHandlerContext,  string,  []interface{},  error) {
	// Calculate processing duration
	if ,  := .Value(startTimeKey).(time.Time);  {
		 := time.Since()
		.ProcessingTimes[] = 
	}

	// Record errors
	if  != nil {
		.ErrorCounts[]++

		// Log error details with connection information
		if ,  := .Conn.(*pool.Conn);  {
			internal.Logger.Printf(, "%s", logs.MetricsHookRecordedError(, .GetID(), ))
		}
	}
}

// GetMetrics returns a summary of collected metrics.
func ( *MetricsHook) () map[string]interface{} {
	return map[string]interface{}{
		"notification_counts": .NotificationCounts,
		"processing_times":    .ProcessingTimes,
		"error_counts":        .ErrorCounts,
	}
}

// ExampleCircuitBreakerMonitor demonstrates how to monitor circuit breaker status
func ( *PoolHook) {
	// Get circuit breaker statistics
	 := .GetCircuitBreakerStats()

	for ,  := range  {
		fmt.Printf("Circuit Breaker for %s:\n", .Endpoint)
		fmt.Printf("  State: %s\n", .State)
		fmt.Printf("  Failures: %d\n", .Failures)
		fmt.Printf("  Last Failure: %v\n", .LastFailureTime)
		fmt.Printf("  Last Success: %v\n", .LastSuccessTime)

		// Alert if circuit breaker is open
		if .State.String() == "open" {
			fmt.Printf("  ⚠️  ALERT: Circuit breaker is OPEN for %s\n", .Endpoint)
		}
	}
}