package cybersecurity

import (
	"net/url"
	"strings"
)

// trackingParams lista de parametros de tracking comunes a eliminar.
var trackingParams = map[string]bool{
	"utm_source":   true,
	"utm_medium":   true,
	"utm_campaign": true,
	"utm_term":     true,
	"utm_content":  true,
	"fbclid":       true,
	"gclid":        true,
	"ref":          true,
	"mc_cid":       true,
	"mc_eid":       true,
}

// NormalizeURL normaliza una URL: convierte el host a minusculas, elimina fragmentos
// y remueve parametros de tracking comunes.
func NormalizeURL(rawURL string) string {
	u, err := url.Parse(rawURL)
	if err != nil {
		return rawURL
	}

	// Lowercase host
	u.Host = strings.ToLower(u.Host)

	// Eliminar fragmento
	u.Fragment = ""

	// Eliminar parametros de tracking
	q := u.Query()
	for param := range trackingParams {
		q.Del(param)
	}
	u.RawQuery = q.Encode()

	return u.String()
}
