package domain

import (
	"regexp"
	"strings"
)

const (
	WebsiteSectionHero         = "hero"
	WebsiteSectionIntro        = "intro"
	WebsiteSectionServices     = "services"
	WebsiteSectionServiceItem  = "service_item"
	WebsiteSectionAbout        = "about"
	WebsiteSectionTeam         = "team"
	WebsiteSectionTestimonials = "testimonials"
	WebsiteSectionCTA          = "cta"
	WebsiteSectionContact      = "contact"
	WebsiteSectionFooter       = "footer"
	WebsiteSectionGallery      = "gallery"
	WebsiteSectionOther        = "other"
)

var websiteSectionOrder = []string{
	WebsiteSectionHero,
	WebsiteSectionIntro,
	WebsiteSectionServices,
	WebsiteSectionServiceItem,
	WebsiteSectionAbout,
	WebsiteSectionTeam,
	WebsiteSectionTestimonials,
	WebsiteSectionCTA,
	WebsiteSectionContact,
	WebsiteSectionFooter,
	WebsiteSectionGallery,
	WebsiteSectionOther,
}

var websiteSectionLabels = map[string]string{
	WebsiteSectionHero:         "Hero",
	WebsiteSectionIntro:        "Intro",
	WebsiteSectionServices:     "Services",
	WebsiteSectionServiceItem:  "Service Item",
	WebsiteSectionAbout:        "About",
	WebsiteSectionTeam:         "Team",
	WebsiteSectionTestimonials: "Testimonials",
	WebsiteSectionCTA:          "CTA",
	WebsiteSectionContact:      "Contact",
	WebsiteSectionFooter:       "Footer",
	WebsiteSectionGallery:      "Gallery",
	WebsiteSectionOther:        "Other",
}

var serviceItemIndexPattern = regexp.MustCompile(`_\d+$`)

func WebsiteSectionOptions() []string {
	out := make([]string, len(websiteSectionOrder))
	copy(out, websiteSectionOrder)
	return out
}

func WebsiteSectionLabel(section string) string {
	normalized := NormalizeWebsiteSection(section)
	if label, ok := websiteSectionLabels[normalized]; ok {
		return label
	}
	return websiteSectionLabels[WebsiteSectionOther]
}

func NormalizeWebsiteSection(section string) string {
	normalized := strings.ToLower(strings.TrimSpace(section))
	for _, candidate := range websiteSectionOrder {
		if normalized == candidate {
			return candidate
		}
	}
	return WebsiteSectionOther
}

func SuggestWebsiteSection(field TemplateField) string {
	section := strings.ToLower(strings.TrimSpace(field.Section))
	key := strings.ToLower(strings.TrimSpace(field.KeyName))
	path := strings.ToLower(strings.TrimSpace(field.Path))
	sample := strings.ToLower(strings.TrimSpace(field.SampleValue))
	fieldKind := strings.ToLower(strings.TrimSpace(field.FieldKind))

	combined := strings.Join([]string{section, key, path}, " ")
	if fieldKind == "image" || containsAny(combined, "gallery", "image", "img", "photo", "picture", "media") {
		return WebsiteSectionGallery
	}
	if containsAny(combined, "testimonial") {
		return WebsiteSectionTestimonials
	}
	if section == "services" || containsAny(combined, "service") {
		if containsAny(combined, "item", "card") || serviceItemIndexPattern.MatchString(key) {
			return WebsiteSectionServiceItem
		}
		return WebsiteSectionServices
	}
	if containsAny(combined, "team", "member", "staff", "employee", "founder") {
		return WebsiteSectionTeam
	}
	if containsAny(combined, "welcome", "headline", "hero", "banner") {
		return WebsiteSectionHero
	}
	if containsAny(combined, "intro", "introduction", "lead") {
		return WebsiteSectionIntro
	}
	if containsAny(combined, "about", "company", "mission", "story") || len(sample) > 180 {
		return WebsiteSectionAbout
	}
	if containsAny(combined, "cta", "highlight", "button", "calltoaction", "call_to_action") {
		return WebsiteSectionCTA
	}
	if containsAny(combined, "contact", "email", "phone", "address") {
		return WebsiteSectionContact
	}
	if containsAny(combined, "footer", "copyright", "imprint", "legal") {
		return WebsiteSectionFooter
	}
	return WebsiteSectionOther
}

func containsAny(value string, needles ...string) bool {
	for _, needle := range needles {
		if strings.Contains(value, needle) {
			return true
		}
	}
	return false
}
