package buildsvc

import "strings"

type GlobalDataInput struct {
	CompanyName      string
	BusinessType     string
	Username         string
	Email            string
	Phone            string
	OrgNumber        string
	StartDate        string
	Mission          string
	DescriptionShort string
	DescriptionLong  string
	SiteLanguage     string
	AddressLine1     string
	AddressLine2     string
	AddressCity      string
	AddressRegion    string
	AddressZIP       string
	AddressCountry   string
}

func BuildGlobalData(input GlobalDataInput) map[string]any {
	globalData := map[string]any{}

	setIfNotEmpty(globalData, "companyName", input.CompanyName)
	setIfNotEmpty(globalData, "businessType", input.BusinessType)
	setIfNotEmpty(globalData, "username", input.Username)
	setIfNotEmpty(globalData, "email", input.Email)
	setIfNotEmpty(globalData, "phone", input.Phone)
	setIfNotEmpty(globalData, "orgNumber", input.OrgNumber)
	setIfNotEmpty(globalData, "startDate", input.StartDate)
	setIfNotEmpty(globalData, "mission", input.Mission)
	setIfNotEmpty(globalData, "descriptionShort", input.DescriptionShort)
	setIfNotEmpty(globalData, "descriptionLong", input.DescriptionLong)
	setIfNotEmpty(globalData, "siteLanguage", input.SiteLanguage)

	address := map[string]any{}
	setIfNotEmpty(address, "line1", input.AddressLine1)
	setIfNotEmpty(address, "line2", input.AddressLine2)
	setIfNotEmpty(address, "city", input.AddressCity)
	setIfNotEmpty(address, "region", input.AddressRegion)
	setIfNotEmpty(address, "zip", input.AddressZIP)
	setIfNotEmpty(address, "country", input.AddressCountry)
	if len(address) > 0 {
		globalData["address"] = address
	}

	return globalData
}

func FilterGlobalData(input map[string]any) map[string]any {
	if len(input) == 0 {
		return map[string]any{}
	}

	globalData := map[string]any{}
	setStringIfPresent(globalData, "companyName", input["companyName"])
	setStringIfPresent(globalData, "businessType", input["businessType"])
	setStringIfPresent(globalData, "username", input["username"])
	setStringIfPresent(globalData, "email", input["email"])
	setStringIfPresent(globalData, "phone", input["phone"])
	setStringIfPresent(globalData, "orgNumber", input["orgNumber"])
	setStringIfPresent(globalData, "startDate", input["startDate"])
	setStringIfPresent(globalData, "mission", input["mission"])
	setStringIfPresent(globalData, "descriptionShort", input["descriptionShort"])
	setStringIfPresent(globalData, "descriptionLong", input["descriptionLong"])
	setStringIfPresent(globalData, "siteLanguage", input["siteLanguage"])

	address := map[string]any{}
	if rawAddress, ok := input["address"]; ok {
		if sourceAddress, ok := rawAddress.(map[string]any); ok {
			setStringIfPresent(address, "line1", sourceAddress["line1"])
			setStringIfPresent(address, "line2", sourceAddress["line2"])
			setStringIfPresent(address, "city", sourceAddress["city"])
			setStringIfPresent(address, "region", sourceAddress["region"])
			setStringIfPresent(address, "zip", sourceAddress["zip"])
			setStringIfPresent(address, "country", sourceAddress["country"])
		}
	}
	if len(address) > 0 {
		globalData["address"] = address
	}

	return globalData
}

func setIfNotEmpty(m map[string]any, key, value string) {
	if strings.TrimSpace(value) == "" {
		return
	}
	m[key] = strings.TrimSpace(value)
}

func setStringIfPresent(target map[string]any, key string, raw any) {
	value, ok := raw.(string)
	if !ok {
		return
	}
	setIfNotEmpty(target, key, value)
}
