package activity

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

// ResourceActivitySourceEventID derives the stable, bounded deduplication key
// for one AGS repository event. It is not an API resource identifier.
func ResourceActivitySourceEventID(repo ResourceActivityDataPlaneRepository, parts ...string) string {
	all := []string{"gitea", strings.TrimSpace(repo.RepoFullName)}
	all = append(all, parts...)
	for idx := range all {
		all[idx] = strings.TrimSpace(all[idx])
	}
	value := strings.Join(all, ":")
	if len(value) <= 240 {
		return value
	}
	sum := sha256.Sum256([]byte(value))
	return "gitea:" + hex.EncodeToString(sum[:])
}

// ResourceActivityControlSourceEventID derives the stable, bounded
// deduplication key for a control-plane repository lifecycle event.
func ResourceActivityControlSourceEventID(resourceType, resourceID, repoRole, event string) string {
	parts := []string{"control", "resource", resourceType, resourceID, "repo", repoRole, event}
	for idx := range parts {
		parts[idx] = strings.TrimSpace(parts[idx])
	}
	value := strings.Join(parts, ":")
	if len(value) <= 240 {
		return value
	}
	sum := sha256.Sum256([]byte(value))
	return "control:resource:" + hex.EncodeToString(sum[:])
}
