package systemconfig

import (
	"strings"
)

func normalizeAnnouncements(items []string) []string {
	normalized := make([]string, 0, len(items))
	for _, item := range items {
		item = strings.TrimSpace(item)
		if item != "" {
			normalized = append(normalized, item)
		}
	}
	if len(normalized) == 0 {
		return defaultHomeAnnouncements()
	}
	return normalized
}

func normalizeHomeBanners(items []HomeBannerItem) []HomeBannerItem {
	normalized := make([]HomeBannerItem, 0, len(items))
	for _, item := range items {
		item.Eyebrow = strings.TrimSpace(item.Eyebrow)
		item.Title = strings.TrimSpace(item.Title)
		item.Badge = strings.TrimSpace(item.Badge)
		item.Pill = strings.TrimSpace(item.Pill)
		item.Tone = strings.TrimSpace(item.Tone)
		item.ImageURL = strings.TrimSpace(item.ImageURL)
		if item.Title == "" && item.ImageURL == "" {
			continue
		}
		if item.Tone != "green" && item.Tone != "orange" {
			item.Tone = "blue"
		}
		normalized = append(normalized, item)
	}
	if len(normalized) == 0 {
		return defaultHomeBanners()
	}
	return normalized
}

func normalizeOrderAgreements(agreements *OrderAgreementsDTO) {
	defaults := DefaultOrderAgreements()
	agreements.VirtualAssetPurchase.Title = strings.TrimSpace(agreements.VirtualAssetPurchase.Title)
	agreements.VirtualAssetPurchase.Content = strings.TrimSpace(agreements.VirtualAssetPurchase.Content)
	agreements.RenterAgreement.Title = strings.TrimSpace(agreements.RenterAgreement.Title)
	agreements.RenterAgreement.Content = strings.TrimSpace(agreements.RenterAgreement.Content)
	if agreements.VirtualAssetPurchase.Title == "" {
		agreements.VirtualAssetPurchase.Title = defaults.VirtualAssetPurchase.Title
	}
	if agreements.VirtualAssetPurchase.Content == "" {
		agreements.VirtualAssetPurchase.Content = defaults.VirtualAssetPurchase.Content
	}
	if agreements.RenterAgreement.Title == "" {
		agreements.RenterAgreement.Title = defaults.RenterAgreement.Title
	}
	if agreements.RenterAgreement.Content == "" {
		agreements.RenterAgreement.Content = defaults.RenterAgreement.Content
	}
}

func normalizeListingPublishAgreements(agreements *ListingPublishAgreementsDTO) {
	defaults := DefaultListingPublishAgreements()
	agreements.VirtualAssetSale.Title = strings.TrimSpace(agreements.VirtualAssetSale.Title)
	agreements.VirtualAssetSale.Content = strings.TrimSpace(agreements.VirtualAssetSale.Content)
	agreements.SellerAgreement.Title = strings.TrimSpace(agreements.SellerAgreement.Title)
	agreements.SellerAgreement.Content = strings.TrimSpace(agreements.SellerAgreement.Content)
	if agreements.VirtualAssetSale.Title == "" {
		agreements.VirtualAssetSale.Title = defaults.VirtualAssetSale.Title
	}
	if agreements.VirtualAssetSale.Content == "" {
		agreements.VirtualAssetSale.Content = defaults.VirtualAssetSale.Content
	}
	if agreements.SellerAgreement.Title == "" {
		agreements.SellerAgreement.Title = defaults.SellerAgreement.Title
	}
	if agreements.SellerAgreement.Content == "" {
		agreements.SellerAgreement.Content = defaults.SellerAgreement.Content
	}
}

func normalizePostRentalNotice(notice *PostRentalNoticeDTO) {
	defaults := DefaultPostRentalNotice()
	notice.Title = strings.TrimSpace(notice.Title)
	notice.Content = strings.TrimSpace(notice.Content)
	if notice.Title == "" {
		notice.Title = defaults.Title
	}
	if notice.Content == "" {
		notice.Content = defaults.Content
	}
}

func normalizePublishOptions(options *PublishOptionsDTO) {
	defaults := DefaultPublishOptions()
	if len(options.ServerOptions) == 0 {
		options.ServerOptions = defaults.ServerOptions
	}
	if len(options.FaceOptions) == 0 {
		options.FaceOptions = defaults.FaceOptions
	}
	if len(options.RankOptions) == 0 {
		options.RankOptions = defaults.RankOptions
	}
	if len(options.InsuranceOptions) == 0 {
		options.InsuranceOptions = defaults.InsuranceOptions
	}
	if len(options.LevelOptions) == 0 {
		options.LevelOptions = defaults.LevelOptions
	}
	if len(options.LoginMethodOptions) == 0 {
		options.LoginMethodOptions = defaults.LoginMethodOptions
	}
	if len(options.RegionOptions) == 0 {
		options.RegionOptions = defaults.RegionOptions
	}
	if len(options.SkinGroups) == 0 {
		options.SkinGroups = defaults.SkinGroups
	}
	if len(options.QuantityItems) == 0 {
		options.QuantityItems = defaults.QuantityItems
	}
	if len(options.ScreenshotSlots) == 0 {
		options.ScreenshotSlots = defaults.ScreenshotSlots
	}
	if len(options.BanRecordOptions) == 0 {
		options.BanRecordOptions = defaults.BanRecordOptions
	}
	if len(options.BanEvidenceOptions) == 0 {
		options.BanEvidenceOptions = defaults.BanEvidenceOptions
	}
	if options.FireLevelMin <= 0 {
		options.FireLevelMin = defaults.FireLevelMin
	}
	if options.PriceConfig.DepositPlaceholder == "" {
		options.PriceConfig.DepositPlaceholder = defaults.PriceConfig.DepositPlaceholder
	}
	if options.PriceConfig.DepositHint == "" {
		options.PriceConfig.DepositHint = defaults.PriceConfig.DepositHint
	}
	if options.PriceConfig.PricePlaceholder == "" {
		options.PriceConfig.PricePlaceholder = defaults.PriceConfig.PricePlaceholder
	}
	if options.PriceConfig.RatioDescription == "" {
		options.PriceConfig.RatioDescription = defaults.PriceConfig.RatioDescription
	}
	if options.DepositRecommend.BaseAmount <= 0 {
		options.DepositRecommend.BaseAmount = defaults.DepositRecommend.BaseAmount
	}
	if len(options.DepositRecommend.SkinGroupRules) == 0 {
		options.DepositRecommend.SkinGroupRules = defaults.DepositRecommend.SkinGroupRules
	}
	if len(options.RatioConfig.InsuranceBaseRatios) == 0 {
		options.RatioConfig.InsuranceBaseRatios = defaults.RatioConfig.InsuranceBaseRatios
	}
	if len(options.RatioConfig.ConfigItems) == 0 {
		options.RatioConfig.ConfigItems = defaults.RatioConfig.ConfigItems
	}
	if len(options.RatioConfig.CoinCorrections) == 0 {
		options.RatioConfig.CoinCorrections = defaults.RatioConfig.CoinCorrections
	}
}

func normalizeSalePriceConfig(config *PublishSalePriceConfig) {
	defaults := DefaultSalePriceConfig()
	if len(config.FixedMarkupRules) == 0 {
		config.FixedMarkupRules = defaults.FixedMarkupRules
	}
	if len(config.RatioAdjustmentRules) == 0 {
		config.RatioAdjustmentRules = defaults.RatioAdjustmentRules
	}
}
