package org.xyzh.workcase.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import org.xyzh.api.workcase.constant.WorkcaseConstant;
import org.xyzh.workcase.listener.ChatMessageListener;

/**
 * @description    Redis Pub/Sub订阅配置
 * @filename    RedisSubscriberConfig.java
 * @author    cascade
 * @copyright xyzh
 * @since 2025-12-22
 */
@Configuration
public class RedisSubscriberConfig {

    @Autowired
    private ChatMessageListener chatMessageListener;

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        // 订阅聊天室消息频道，使用通配符匹配所有聊天室
        // 频道格式: chat:room:*
        container.addMessageListener(chatMessageListener, 
                new PatternTopic(WorkcaseConstant.REDIS_CHAT_PREFIX + "*"));

        // 订阅聊天室列表更新频道
        container.addMessageListener(chatMessageListener,
                new PatternTopic(WorkcaseConstant.REDIS_CHAT_LIST_UPDATE));

        return container;
    }
}
