package logger

import (
	"testing"
)

func TestRedactor(t *testing.T) {
	tests := []struct {
		name     string
		enabled  bool
		input    string
		expected string
	}{
		{
			name:     "redaction disabled",
			enabled:  false,
			input:    "password='secret123'",
			expected: "password='secret123'",
		},
		{
			name:     "password with equals",
			enabled:  true,
			input:    "password='secret123'",
			expected: "password=[REDACTED]",
		},
		{
			name:     "password with colon",
			enabled:  true,
			input:    "password: 'secret123'",
			expected: "password: [REDACTED]",
		},
		{
			name:     "connection string",
			enabled:  true,
			input:    "mysql://user:password123@localhost/db",
			expected: "mysql://user:[REDACTED]@localhost/db",
		},
		{
			name:     "token",
			enabled:  true,
			input:    "token='abc123xyz'",
			expected: "token=[REDACTED]",
		},
		{
			name:     "multiple patterns",
			enabled:  true,
			input:    "INSERT INTO users (username, password) VALUES ('john', 'secret123')",
			expected: "INSERT INTO users (username, password) VALUES ('john', 'secret123')",
		},
		{
			name:     "case insensitive",
			enabled:  true,
			input:    "PASSWORD='Secret123'",
			expected: "PASSWORD=[REDACTED]",
		},
		{
			name:     "bearer token",
			enabled:  true,
			input:    "Authorization: Bearer abc123xyz",
			expected: "Authorization: Bearer [REDACTED]",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			redactor := NewRedactor(tt.enabled, []string{})
			result := redactor.RedactQuery(tt.input)
			if result != tt.expected {
				t.Errorf("RedactQuery(%q) = %q, want %q", tt.input, result, tt.expected)
			}
		})
	}
}

func TestRedactorWithCustomPatterns(t *testing.T) {
	customPattern := `(custom_key\s*=\s*)['"][^'"]*['"]`
	redactor := NewRedactor(true, []string{customPattern})

	input := "custom_key='my_secret_value'"
	result := redactor.RedactQuery(input)
	expected := "custom_key=[REDACTED]"

	if result != expected {
		t.Errorf("RedactQuery with custom pattern = %q, want %q", result, expected)
	}
}

func TestRedactorDisabled(t *testing.T) {
	redactor := NewRedactor(false, []string{})

	// Should not redact anything
	input := "password='secret' token='abc'"
	result := redactor.RedactQuery(input)

	if result != input {
		t.Errorf("RedactQuery with disabled redactor changed input: got %q, want %q", result, input)
	}
}

func TestRedactorMultipleReplacements(t *testing.T) {
	redactor := NewRedactor(true, []string{})

	input := "password='secret1' token='abc2' password: 'secret3'"
	result := redactor.RedactQuery(input)
	expected := "password=[REDACTED] token=[REDACTED] password: [REDACTED]"

	if result != expected {
		t.Errorf("RedactQuery multiple = %q, want %q", result, expected)
	}
}
