package kvstore

// KV Store key prefixes and constants
// This file centralizes all KV store key patterns used throughout the plugin
// to ensure consistency and avoid key conflicts.

const (
	// CurrentKVStoreVersion is the current version requiring migrations
	CurrentKVStoreVersion = 2
	// KeyPrefixXMPPUser is the prefix for XMPP user ID -> Mattermost user ID mappings
	KeyPrefixXMPPUser = "xmpp_user_"
	// KeyPrefixMattermostUser is the prefix for Mattermost user ID -> XMPP user ID mappings
	KeyPrefixMattermostUser = "mattermost_user_"

	// KeyPrefixChannelMapping is the prefix for Mattermost channel ID -> XMPP room mappings
	KeyPrefixChannelMapping = "channel_mapping_"
	// KeyPrefixRoomMapping is the prefix for XMPP room identifier -> Mattermost channel ID mappings
	KeyPrefixRoomMapping = "xmpp_room_mapping_"

	// KeyPrefixGhostUser is the prefix for Mattermost user ID -> XMPP ghost user ID cache
	KeyPrefixGhostUser = "ghost_user_"
	// KeyPrefixGhostRoom is the prefix for ghost user room membership tracking
	KeyPrefixGhostRoom = "ghost_room_"

	// KeyPrefixXMPPEventPost is the prefix for XMPP event ID -> Mattermost post ID mappings
	KeyPrefixXMPPEventPost = "xmpp_event_post_"
	// KeyPrefixXMPPReaction is the prefix for XMPP reaction event ID -> reaction info mappings
	KeyPrefixXMPPReaction = "xmpp_reaction_"

	// KeyStoreVersion is the key for tracking the current KV store schema version
	KeyStoreVersion = "kv_store_version"

	// KeyPrefixLegacyDMMapping was the old prefix for DM mappings (migrated to channel_mapping_)
	KeyPrefixLegacyDMMapping = "dm_mapping_"
	// KeyPrefixLegacyXMPPDMMapping was the old prefix for XMPP DM mappings (migrated to room_mapping_)
	KeyPrefixLegacyXMPPDMMapping = "xmpp_dm_mapping_"
)

// Helper functions for building KV store keys

// BuildXMPPUserKey creates a key for XMPP user -> Mattermost user mapping
func BuildXMPPUserKey(xmppUserID string) string {
	return KeyPrefixXMPPUser + xmppUserID
}

// BuildMattermostUserKey creates a key for Mattermost user -> XMPP user mapping
func BuildMattermostUserKey(mattermostUserID string) string {
	return KeyPrefixMattermostUser + mattermostUserID
}

// BuildChannelMappingKey creates a key for channel -> room mapping
func BuildChannelMappingKey(channelID string) string {
	return KeyPrefixChannelMapping + channelID
}

// BuildRoomMappingKey creates a key for room -> channel mapping
func BuildRoomMappingKey(roomIdentifier string) string {
	return KeyPrefixRoomMapping + roomIdentifier
}

// BuildGhostUserKey creates a key for ghost user cache
func BuildGhostUserKey(mattermostUserID string) string {
	return KeyPrefixGhostUser + mattermostUserID
}

// BuildGhostRoomKey creates a key for ghost user room membership
func BuildGhostRoomKey(mattermostUserID, roomID string) string {
	return KeyPrefixGhostRoom + mattermostUserID + "_" + roomID
}

// BuildXMPPEventPostKey creates a key for XMPP event -> post mapping
func BuildXMPPEventPostKey(xmppEventID string) string {
	return KeyPrefixXMPPEventPost + xmppEventID
}

// BuildXMPPReactionKey creates a key for XMPP reaction storage
func BuildXMPPReactionKey(reactionEventID string) string {
	return KeyPrefixXMPPReaction + reactionEventID
}

// ExtractChannelIDFromKey extracts the channel ID from a channel mapping key
func ExtractChannelIDFromKey(key string) string {
	if len(key) <= len(KeyPrefixChannelMapping) {
		return ""
	}
	return key[len(KeyPrefixChannelMapping):]
}
