package event

import (
	"crypto/sha256"
	"encoding/hex"
)

// maxGroupKeyLen matches the event.group_key column (VARCHAR(255)).
const maxGroupKeyLen = 255

// NormalizeGroupKey fits an alertmanager group key into the column.
// Real group keys are full grouping expressions and can exceed 255 chars;
// long keys keep a readable prefix plus a deterministic hash suffix, so
// events of the same group still share one key.
func NormalizeGroupKey(key string) string {
	if len(key) <= maxGroupKeyLen {
		return key
	}
	sum := sha256.Sum256([]byte(key))
	suffix := "#" + hex.EncodeToString(sum[:8])
	return key[:maxGroupKeyLen-len(suffix)] + suffix
}
