package constants

import contractconstants "github.com/pingcap-inc/nexuscrm-contract/pkg/constants"

// ContextKey is a typed key for context values to prevent collisions with other packages.
// See: https://pkg.go.dev/context#WithValue
type ContextKey = contractconstants.ContextKey

const (
	// ContextKeyTraceparent is the context key for W3C traceparent header (standard)
	ContextKeyTraceparent ContextKey = "traceparent"

	// ContextKeyRequestID is the context key for the unique request identifier
	ContextKeyRequestID ContextKey = contractconstants.ContextKeyRequestID

	// ContextKeyAfterSaveChainID is the context key for the AfterSave re-entrancy
	// chain identifier. Propagated through outbox Metadata to bound recursion depth
	// to one causal chain rooted at a single user-initiated save, not a process-level
	// TTL bucket shared across independent user saves.
	ContextKeyAfterSaveChainID ContextKey = "after_save_chain_id"

	// ContextKeyAfterSaveChainDepth is the context key for the AfterSave re-entrancy
	// chain depth: 0 for the root save, +1 for each subsequent self-DML outbox hop.
	ContextKeyAfterSaveChainDepth ContextKey = "after_save_chain_depth"

	// ContextKeyAfterSaveTupleCounts is the context key for the JSON-encoded map
	// of record-trigger tuple execution counts within one AfterSave causal chain.
	// Key shape is recordTriggerFlowKey(keyPrefix, recordID, flowDefinitionID).
	ContextKeyAfterSaveTupleCounts ContextKey = "after_save_tuple_counts"

	// ContextKeyConnectorSyncOrigin marks connector-originated writes. The outbox
	// carries it as internal metadata so connector outbound actions can suppress
	// remote echo loops without changing public record payload shape.
	ContextKeyConnectorSyncOrigin ContextKey = contractconstants.ContextKeyConnectorSyncOrigin

	// ContextKeySuppressRecordTriggeredAutomation marks system-owned side effects
	// that should still emit record events for audit/streaming observers, but
	// must not dispatch record-triggered automation for the side-effect row.
	ContextKeySuppressRecordTriggeredAutomation ContextKey = "suppress_record_triggered_automation"

	// InternalMetadataKeyConnectorSyncOrigin is the serialized outbox metadata
	// key for ContextKeyConnectorSyncOrigin.
	InternalMetadataKeyConnectorSyncOrigin = string(ContextKeyConnectorSyncOrigin)

	// InternalMetadataKeySuppressRecordTriggeredAutomation is the serialized
	// outbox metadata key for ContextKeySuppressRecordTriggeredAutomation.
	InternalMetadataKeySuppressRecordTriggeredAutomation = string(ContextKeySuppressRecordTriggeredAutomation)

	// InternalMetadataKeyOriginatingSessionID is the serialized outbox metadata
	// key for the validated session selector. It is deliberately internal: an
	// asynchronous effect may use it to re-authorize as the initiating user,
	// but event subscribers must never receive it as public record metadata.
	// The selector is a JWT/session identifier, never a raw bearer token.
	InternalMetadataKeyOriginatingSessionID = string(ContextKeySessionID)

	// ContextKeyPerfTiming enables per-request performance timing instrumentation.
	// When present and true on the context, hot-path timing logs are emitted.
	// Default (absent): no timing logs are produced.
	ContextKeyPerfTiming ContextKey = contractconstants.ContextKeyPerfTiming
)
