package utils

import (
	"testing"
)

func TestSanitizeString(t *testing.T) {
	s := DefaultSanitizer()

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{"simple text", "hello world", "hello world"},
		{"with whitespace", "  hello  ", "hello"},
		{"with html", "<script>alert('xss')</script>", "&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;"},
		{"empty string", "", ""},
		{"special chars", "café & thé", "café &amp; thé"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := s.SanitizeString(tt.input)
			if result != tt.expected {
				t.Errorf("SanitizeString(%q) = %q, want %q", tt.input, result, tt.expected)
			}
		})
	}
}

func TestSanitizeSlug(t *testing.T) {
	s := DefaultSanitizer()

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{"simple text", "Hello World", "hello-world"},
		{"special chars", "Café & Thé!", "caf-th"},
		{"multiple spaces", "hello   world", "hello-world"},
		{"already slug", "hello-world", "hello-world"},
		{"numbers", "test 123", "test-123"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := s.SanitizeSlug(tt.input)
			if result != tt.expected {
				t.Errorf("SanitizeSlug(%q) = %q, want %q", tt.input, result, tt.expected)
			}
		})
	}
}

func TestSanitizeName(t *testing.T) {
	s := DefaultSanitizer()
	s.MaxNameLength = 10

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{"short name", "John", "John"},
		{"max length", "1234567890", "1234567890"},
		{"over limit", "12345678901", "1234567890"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := s.SanitizeName(tt.input)
			if result != tt.expected {
				t.Errorf("SanitizeName(%q) = %q, want %q", tt.input, result, tt.expected)
			}
		})
	}
}

func TestStripHTML(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{"simple html", "<p>hello</p>", "hello"},
		{"script tag", "<script>alert('xss')</script>", "alert('xss')"},
		{"nested tags", "<div><span>text</span></div>", "text"},
		{"no html", "plain text", "plain text"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := StripHTML(tt.input)
			if result != tt.expected {
				t.Errorf("StripHTML(%q) = %q, want %q", tt.input, result, tt.expected)
			}
		})
	}
}
