package org.xyzh.ai.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.ai.listener.DifyConfigListener;

/**
 * AI模块Redis订阅配置
 *
 * @author cascade
 * @since 2026-01-01
 */
@Configuration
public class RedisSubscriberConfig {

    @Autowired
    private DifyConfigListener difyConfigListener;

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

        // 订阅Dify配置变更频道
        container.addMessageListener(difyConfigListener, 
                new PatternTopic(difyConfigListener.getChannelPattern()));

        return container;
    }
}
