package buildsvc

import "testing"

func TestBuildGlobalData_OptionalFieldsAreOmittedWhenEmpty(t *testing.T) {
	got := BuildGlobalData(GlobalDataInput{
		CompanyName:  "Acme AG",
		Email:        "hello@acme.test",
		Username:     "acme-admin",
		AddressLine1: "  ",
	})

	if got["companyName"] != "Acme AG" {
		t.Fatalf("companyName mismatch: got=%v", got["companyName"])
	}
	if got["email"] != "hello@acme.test" {
		t.Fatalf("email mismatch: got=%v", got["email"])
	}
	if got["username"] != "acme-admin" {
		t.Fatalf("username mismatch: got=%v", got["username"])
	}
	if _, ok := got["phone"]; ok {
		t.Fatalf("phone should be omitted when empty")
	}
	if _, ok := got["address"]; ok {
		t.Fatalf("address should be omitted when empty")
	}
}

func TestBuildGlobalData_AddressAndOptionalFields(t *testing.T) {
	got := BuildGlobalData(GlobalDataInput{
		CompanyName:      "Acme AG",
		BusinessType:     "Healthcare",
		Username:         "acme-admin",
		Email:            "hello@acme.test",
		Phone:            "+41 79 000 00 00",
		OrgNumber:        "CHE-123.456.789",
		StartDate:        "2019-08-01",
		Mission:          "Make dental care simple.",
		DescriptionShort: "Modern clinic.",
		DescriptionLong:  "Full-service clinic in central Zurich.",
		SiteLanguage:     "de",
		AddressLine1:     "Main Street 1",
		AddressLine2:     "2nd Floor",
		AddressCity:      "Zurich",
		AddressRegion:    "ZH",
		AddressZIP:       "8000",
		AddressCountry:   "Switzerland",
	})

	addressRaw, ok := got["address"]
	if !ok {
		t.Fatalf("address should be present")
	}
	address, ok := addressRaw.(map[string]any)
	if !ok {
		t.Fatalf("address type mismatch: %T", addressRaw)
	}
	if address["line1"] != "Main Street 1" {
		t.Fatalf("line1 mismatch: got=%v", address["line1"])
	}
	if address["line2"] != "2nd Floor" {
		t.Fatalf("line2 mismatch: got=%v", address["line2"])
	}
	if address["region"] != "ZH" {
		t.Fatalf("region mismatch: got=%v", address["region"])
	}
	if got["businessType"] != "Healthcare" {
		t.Fatalf("businessType mismatch: got=%v", got["businessType"])
	}
	if got["phone"] != "+41 79 000 00 00" {
		t.Fatalf("phone mismatch: got=%v", got["phone"])
	}
	if got["siteLanguage"] != "de" {
		t.Fatalf("siteLanguage mismatch: got=%v", got["siteLanguage"])
	}
	if _, ok := got["business_category"]; ok {
		t.Fatalf("business_category must not be present")
	}
}

func TestFilterGlobalData_RemovesUnsupportedKeys(t *testing.T) {
	got := FilterGlobalData(map[string]any{
		"companyName":       " Acme AG ",
		"username":          " admin ",
		"email":             " hi@example.test ",
		"mobile":            "+41 79 000 00 00",
		"business_category": "Healthcare",
		"siteLanguage":      " de ",
		"address": map[string]any{
			"line1":        " Main Street 1 ",
			"line2":        "",
			"city":         " Zurich ",
			"region":       " ZH ",
			"zip":          " 8000 ",
			"country":      " Switzerland ",
			"country_code": "CH",
			"street":       "Unsupported Street Key",
		},
	})

	if got["companyName"] != "Acme AG" {
		t.Fatalf("companyName mismatch: got=%v", got["companyName"])
	}
	if got["siteLanguage"] != "de" {
		t.Fatalf("siteLanguage mismatch: got=%v", got["siteLanguage"])
	}
	if _, ok := got["mobile"]; ok {
		t.Fatalf("mobile must not be present")
	}
	if _, ok := got["business_category"]; ok {
		t.Fatalf("business_category must not be present")
	}

	addressRaw, ok := got["address"]
	if !ok {
		t.Fatalf("address should be present")
	}
	address, ok := addressRaw.(map[string]any)
	if !ok {
		t.Fatalf("address type mismatch: %T", addressRaw)
	}
	if address["line1"] != "Main Street 1" {
		t.Fatalf("line1 mismatch: got=%v", address["line1"])
	}
	if _, ok := address["country_code"]; ok {
		t.Fatalf("address.country_code must not be present")
	}
	if _, ok := address["street"]; ok {
		t.Fatalf("address.street must not be present")
	}
}
