package placement

import (
	"strings"
)

func ScoreProductForTag(searchTag string, matchTags []string) int {
	tag := strings.TrimSpace(strings.ToLower(searchTag))
	if tag == "" || len(matchTags) == 0 {
		return 0
	}
	best := 0
	for _, raw := range matchTags {
		candidate := strings.TrimSpace(strings.ToLower(raw))
		if candidate == "" {
			continue
		}
		switch {
		case tag == candidate:
			best = maxInt(best, 100)
		case strings.Contains(tag, candidate) || strings.Contains(candidate, tag):
			best = maxInt(best, 70)
		default:
			tagWords := strings.Fields(tag)
			candWords := strings.Fields(candidate)
			overlap := 0
			for _, w := range tagWords {
				for _, c := range candWords {
					if strings.Contains(c, w) || strings.Contains(w, c) {
						overlap++
						break
					}
				}
			}
			if overlap > 0 {
				best = maxInt(best, 40+overlap*10)
			}
		}
	}
	return best
}

func maxInt(a, b int) int {
	if a > b {
		return a
	}
	return b
}
