package benchmarks

import (
	"testing"

	"github.com/jfraeys/fetch_ml/internal/logging"
)

// BenchmarkLogSanitizeMessage profiles log message sanitization.
// This is a Tier 1 C++ candidate because:
//   - Regex matching is CPU-intensive
//   - High volume log pipelines process thousands of messages/sec
//   - C++ can use Hyperscan/RE2 for parallel regex matching
//
// Expected speedup: 3-5x for high-volume logging
func BenchmarkLogSanitizeMessage(b *testing.B) {
	// Test messages with various sensitive data patterns
	messages := []string{
		"User login successful with api_key=abc123def45678901234567890abcdef",
		"JWT token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0C12LVE",
		"Redis connection: redis://:secretpassword123@localhost:6379/0",
		"User admin password=supersecret123 trying to access resource",
		"Normal log message without any sensitive data to process",
		"API call with key=fedcba9876543210fedcba9876543210 and secret=shh123",
		"Connection string: redis://:another_secret@redis.example.com:6380",
		"Authentication token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.test.signature",
	}

	b.ReportAllocs()

	for i := 0; b.Loop(); i++ {
		msg := messages[i%len(messages)]
		_ = logging.SanitizeLogMessage(msg)
	}
}

// BenchmarkLogSanitizeArgs profiles structured log argument sanitization.
// This processes key-value pairs looking for sensitive field names.
func BenchmarkLogSanitizeArgs(b *testing.B) {
	// Simulate typical structured log arguments
	args := []any{
		"user_id", "user123",
		"password", "secret123",
		"api_key", "abcdef1234567890",
		"action", "login",
		"secret_token", "eyJhbGci...",
		"request_id", "req-12345",
		"database_url", "redis://:password@localhost:6379",
		"timestamp", "2024-01-01T00:00:00Z",
	}

	b.ReportAllocs()

	for b.Loop() {
		_ = logging.SanitizeArgs(args)
	}
}

// BenchmarkLogSanitizeHighVolume simulates high-throughput logging scenario
// with many messages per second (e.g., 10K+ messages/sec).
func BenchmarkLogSanitizeHighVolume(b *testing.B) {
	// Mix of message types
	testMessages := []string{
		"API request: POST /api/v1/jobs with api_key=abcdef1234567890abcdef1234567890",
		"User user123 authenticated with token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature",
		"Database connection established to redis://:redact_me@localhost:6379",
		"Job execution started for job_name=test_job",
		"Error processing request: password=wrong_secret provided",
		"Metrics: cpu=45%, memory=2.5GB, gpu=0%",
		"Config loaded: secret_key=hidden_value123",
		"Webhook received with authorization=Bearer eyJ0eXAiOiJKV1Qi.test.sig",
	}

	b.ReportAllocs()

	for b.Loop() {
		// Simulate batch processing of multiple messages
		for _, msg := range testMessages {
			_ = logging.SanitizeLogMessage(msg)
		}
	}
}
