package tech.easyflow.common.mq.redis;

import tech.easyflow.common.mq.config.MQProperties;

public class RedisStreamKeySupport {

    private final MQProperties properties;

    public RedisStreamKeySupport(MQProperties properties) {
        this.properties = properties;
    }

    public String streamKey(String topic, int shard) {
        return properties.getRedis().getStreamPrefix() + ":" + topic + ":" + String.format("%02d", shard);
    }

    public String deadLetterKey(String topic) {
        return properties.getRedis().getStreamPrefix() + ":dead-letter:" + topic;
    }

    public int resolveShard(String key, int shardCount) {
        if (shardCount <= 0) {
            return 0;
        }
        return Math.floorMod(key == null ? 0 : key.hashCode(), shardCount);
    }
}
