package rediskeys

import (
	"errors"
	"fmt"
	"strings"
)

// WorkerKey builds a Redis key in the format "<prefix>:worker:<purpose>:<id>"
// prefix, purpose, and id are trimmed of whitespace and leading/trailing colons.
// If purpose or id is empty after normalization, an error is returned.
func WorkerKey(prefix, purpose, id string) (string, error) {
	normPrefix := strings.Trim(prefix, " \t\n\r:")
	if normPrefix == "" {
		normPrefix = "alt"
	}
	normPrefix = strings.ToLower(normPrefix)

	normPurpose := strings.Trim(purpose, " \t\n\r:")
	if normPurpose == "" {
		return "", errors.New("rediskeys: purpose cannot be empty")
	}
	normPurpose = strings.ToLower(normPurpose)

	normID := strings.Trim(id, " \t\n\r:")
	if normID == "" {
		return "", errors.New("rediskeys: id cannot be empty")
	}

	return fmt.Sprintf("%s:worker:%s:%s", normPrefix, normPurpose, normID), nil
}
