package listing

func hasAdminAssetFilters(query AdminListQuery) bool {
	return len(query.Insurance) > 0 ||
		len(query.Stamina) > 0 ||
		len(query.Load) > 0 ||
		len(query.SkinGroup) > 0 ||
		len(query.SkinName) > 0
}

func filterAdminListings(items []ListingDTO, query AdminListQuery) []ListingDTO {
	if !hasAdminAssetFilters(query) {
		return items
	}
	filtered := make([]ListingDTO, 0, len(items))
	for _, item := range items {
		if !matchesAdminAssetQuery(item, query) {
			continue
		}
		filtered = append(filtered, item)
	}
	return filtered
}

func matchesAdminAssetQuery(item ListingDTO, query AdminListQuery) bool {
	if !matchesAny(query.Insurance, readAssetString(item.AssetSummary, "season_insurance")) {
		return false
	}
	if !matchesAny(query.Stamina, readAssetString(item.AssetSummary, "stamina_level")) {
		return false
	}
	if !matchesAny(query.Load, readAssetString(item.AssetSummary, "load_level")) {
		return false
	}
	if len(query.SkinName) > 0 {
		if len(query.SkinGroup) > 0 {
			return skinGroupsContainAny(item.AssetSummary, query.SkinGroup, query.SkinName)
		}
		return intersects(query.SkinName, skinNamesFromSummary(item.AssetSummary))
	}
	if len(query.SkinGroup) > 0 {
		return skinGroupsHaveAny(item.AssetSummary, query.SkinGroup)
	}
	return true
}
