package websearch

import (
	"fmt"
	"strings"
)

func BuildContactSearchQuery(firstName, lastName, middleName, company, jobTitle, city string) string {
	var parts []string
	name := strings.TrimSpace(strings.Join([]string{
		strings.TrimSpace(firstName),
		strings.TrimSpace(middleName),
		strings.TrimSpace(lastName),
	}, " "))
	if name != "" {
		parts = append(parts, name)
	}
	if company = strings.TrimSpace(company); company != "" {
		parts = append(parts, company)
	}
	if jobTitle = strings.TrimSpace(jobTitle); jobTitle != "" {
		parts = append(parts, jobTitle)
	}
	if city = strings.TrimSpace(city); city != "" {
		parts = append(parts, city)
	}
	return strings.Join(parts, " ")
}

func FormatResultsForPrompt(results []Result) string {
	if len(results) == 0 {
		return ""
	}
	var b strings.Builder
	b.WriteString("Résultats de recherche en ligne, attention ces resultats peuvent n'avoir aucun lien avec le contact ou concerner des homonymes:\n")
	for i, r := range results {
		fmt.Fprintf(&b, "\n%d. Titre: %s\n   URL: %s\n   Description: %s", i+1, r.Title, r.URL, r.Description)
	}
	return b.String()
}
