package utils

import (
	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
	"testing"
)

func TestStripEmail(t *testing.T) {
	tests := []struct {
		name         string
		email        string
		wantStripped string
		wantDomain   string
	}{
		{
			name:         "Test with + symbol",
			email:        "test+extra@gmail.com",
			wantStripped: "test@gmail.com",
			wantDomain:   "gmail.com",
		},
		{
			name:         "Test without + symbol",
			email:        "test@gmail.com",
			wantStripped: "test@gmail.com",
			wantDomain:   "gmail.com",
		},
		{
			name:         "Test with multiple + symbols",
			email:        "test+extra+more@gmail.com",
			wantStripped: "test@gmail.com",
			wantDomain:   "gmail.com",
		},
		{
			name:         "Test with different domain",
			email:        "test+extra@yahoo.com",
			wantStripped: "test@yahoo.com",
			wantDomain:   "yahoo.com",
		},
		{
			name:         "Test with subdomain",
			email:        "test+extra@mail.example.com",
			wantStripped: "test@mail.example.com",
			wantDomain:   "mail.example.com",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotStripped, gotDomain := StripEmail(tt.email)
			if gotStripped != tt.wantStripped {
				t.Errorf("StripEmail() gotStripped = %v, want %v", gotStripped, tt.wantStripped)
			}
			if gotDomain != tt.wantDomain {
				t.Errorf("StripEmail() gotDomain = %v, want %v", gotDomain, tt.wantDomain)
			}
		})
	}
}

func TestNormalizeEmail(t *testing.T) {
	tests := []struct {
		name           string
		email          string
		wantNormalized string
	}{
		{
			name:           "Test with + symbol",
			email:          "test+extra@gmail.com",
			wantNormalized: "test@gmail.com",
		},
		{
			name:           "Test without + symbol",
			email:          "test@gmail.com",
			wantNormalized: "test@gmail.com",
		},
		{
			name:           "Test with multiple + symbols",
			email:          "test+extra+more@gmail.com",
			wantNormalized: "test@gmail.com",
		},
		{
			name:           "Test with different domain",
			email:          "test-extra@yahoo.com",
			wantNormalized: "testextra@yahoo.com",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotNormalized := NormalizeEmail(tt.email)
			if gotNormalized != tt.wantNormalized {
				t.Errorf("NormalizeEmail() gotNormalized = %v, want %v", gotNormalized, tt.wantNormalized)
			}
		})
	}
}

func TestValidateIPAddress(t *testing.T) {
	tests := []struct {
		name string
		ip   string
		want string
		err  error
	}{
		{
			name: "Test with valid IPv4",
			ip:   "192.168.1.1",
			want: "192.168.1.1",
			err:  nil,
		},
		{
			name: "Test with valid IPv6",
			ip:   "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
			want: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
			err:  nil,
		},
		{
			name: "Test with invalid IP",
			ip:   "999.999.999.999",
			want: "",
			err:  errors.Error("invalid IP address"),
		},
		{
			name: "Test with empty string",
			ip:   "",
			want: "",
			err:  errors.Error("invalid IP address"),
		},
		{
			name: "Test with non-IP string",
			ip:   "not an ip address",
			want: "",
			err:  errors.Error("invalid IP address"),
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := ValidateIPAddress(tt.ip)
			if got != tt.want {
				t.Errorf("ValidateIPAddress() got = %v, want %v", got, tt.want)
			}
			if err != nil && err.Error() != tt.err.Error() {
				t.Errorf("ValidateIPAddress() err = %v, want %v", err, tt.err)
			}
		})
	}
}