package store

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

// BuildArtifactID returns the deterministic artifact ID for a parsed file.
func BuildArtifactID(snapshotID, repoID, commitSHA, filePath string) string {
	key := repoID + "|" + snapshotID + "|" + commitSHA + "|" + filePath
	sum := sha256.Sum256([]byte(key))
	return fmt.Sprintf("art_%s_%s", snapshotID, hex.EncodeToString(sum[:4]))
}

// BuildDeltaID returns the deterministic delta artifact ID for a commit pair.
func BuildDeltaID(repoID, baseCommitSHA, targetCommitSHA string) string {
	key := repoID + "|" + baseCommitSHA + "|" + targetCommitSHA
	sum := sha256.Sum256([]byte(key))
	prefix := targetCommitSHA
	if len(prefix) > 8 {
		prefix = prefix[:8]
	}
	return fmt.Sprintf("delta_%s_%s", prefix, hex.EncodeToString(sum[:4]))
}

// BuildMergedSnapshotArtifactID returns the artifact ID for a finalized snapshot graph.
func BuildMergedSnapshotArtifactID(snapshotID string) string {
	return fmt.Sprintf("art_%s_merged", snapshotID)
}

// MongoURI returns the mongo:// collection/id URI for pipeline events.
func MongoURI(collection, id string) string {
	return fmt.Sprintf("mongo://%s/%s", collection, id)
}
