package domain

import (
	"testing"
)

func TestValidateLoginID(t *testing.T) {
	tests := []struct {
		name    string
		loginID string
		emails  []string
		phone   string
		wantErr bool
	}{
		{"Empty", "", []string{"test@email.com"}, "01012345678", false},
		{"Valid alphanumeric", "user123", []string{"test@email.com"}, "01012345678", false},
		{"Too short", "us", []string{"test@email.com"}, "01012345678", true},
		{"Too long", "thisisaverylongloginidthatiswayoverthirtycharacters", []string{"test@email.com"}, "01012345678", true},
		{"Email format", "user@domain.com", []string{"test@email.com"}, "01012345678", true},
		{"Exact email match", "Test@Email.Com", []string{"test@email.com"}, "01012345678", true},
		{"Secondary email match", "sub@test.com", []string{"test@email.com", "sub@test.com"}, "01012345678", true},
		{"Phone number match", "010-1234-5678", []string{"test@email.com"}, "01012345678", true},
		{"Phone number match +82", "+821012345678", []string{"test@email.com"}, "01012345678", true},
		{"Phone number match digits", "01012345678", []string{"test@email.com"}, "01012345678", true},
		{"Phone format (11 digits)", "01098765432", []string{"test@email.com"}, "01012345678", true},
		{"Valid pure digits (employee ID)", "20230001", []string{"test@email.com"}, "01012345678", false},
		{"Valid pure digits long", "123456789", []string{"test@email.com"}, "01012345678", false},
		{"Valid pure digits 10 chars", "1234567890", []string{"test@email.com"}, "01012345678", false},
		{"Reserved word admin", "ADMIN", []string{"test@email.com"}, "01012345678", true},
		{"Reserved word root", "root", []string{"test@email.com"}, "01012345678", true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := ValidateLoginID(tt.loginID, tt.emails, tt.phone)
			if (err != nil) != tt.wantErr {
				t.Errorf("ValidateLoginID() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestNormalizePhoneNumberDeduplicatesKoreanCountryCode(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{"Local mobile", "010-9191-7771", "+821091917771"},
		{"Korean country code", "+82 10-9191-7771", "+821091917771"},
		{"Duplicate plus Korean country code", "+82 +821091917771", "+821091917771"},
		{"Duplicate compact Korean country code", "+82821091917771", "+821091917771"},
		{"Duplicate spaced Korean country code", "+82 8210 9191 7771", "+821091917771"},
		{"Non Korean international phone preserved", "+1 914 481 2222", "+19144812222"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := NormalizePhoneNumber(tt.input); got != tt.want {
				t.Fatalf("NormalizePhoneNumber(%q)=%q, want %q", tt.input, got, tt.want)
			}
		})
	}
}
