package domain

import "testing"

func TestSplitEmailDomainAllowsDomainOnlyImportInput(t *testing.T) {
	tests := []struct {
		name       string
		email      string
		wantLocal  string
		wantDomain string
	}{
		{name: "full address", email: " Han@SamanEng.com ", wantLocal: "han", wantDomain: "samaneng.com"},
		{name: "domain only", email: "@hanmaceng.co.kr", wantLocal: "", wantDomain: "hanmaceng.co.kr"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			local, domain, err := SplitEmailDomain(tt.email)
			if err != nil {
				t.Fatalf("SplitEmailDomain() error = %v", err)
			}
			if local != tt.wantLocal || domain != tt.wantDomain {
				t.Fatalf("SplitEmailDomain() = (%q, %q), want (%q, %q)", local, domain, tt.wantLocal, tt.wantDomain)
			}
		})
	}
}

func TestBuildKoreanNameEmailBase(t *testing.T) {
	base, needsReview, err := BuildKoreanNameEmailBase("한치영")
	if err != nil {
		t.Fatalf("BuildKoreanNameEmailBase() error = %v", err)
	}
	if needsReview {
		t.Fatalf("BuildKoreanNameEmailBase() needsReview = true")
	}
	if base != "cyhan" {
		t.Fatalf("BuildKoreanNameEmailBase() = %q, want %q", base, "cyhan")
	}
}

func TestBuildKoreanNameEmailBaseNeedsReviewForUnknownName(t *testing.T) {
	base, needsReview, err := BuildKoreanNameEmailBase("A치영")
	if err != nil {
		t.Fatalf("BuildKoreanNameEmailBase() error = %v", err)
	}
	if base != "" {
		t.Fatalf("BuildKoreanNameEmailBase() base = %q, want empty", base)
	}
	if !needsReview {
		t.Fatalf("BuildKoreanNameEmailBase() needsReview = false")
	}
}

func TestMatchesSuggestedNameRule(t *testing.T) {
	tests := []struct {
		localPart string
		base      string
		want      bool
	}{
		{localPart: "cyhan", base: "cyhan", want: true},
		{localPart: "cyhan1", base: "cyhan", want: true},
		{localPart: "cyhan20", base: "cyhan", want: true},
		{localPart: "hcy", base: "cyhan", want: false},
		{localPart: "han.cy", base: "cyhan", want: false},
		{localPart: "cyhan-a", base: "cyhan", want: false},
	}

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