package com.iqser.red.service.persistence.management.v1.processor.cache;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
public class RedisConfiguration {

    @Bean
    RedisStandaloneConfiguration redisStandaloneConfiguration(Environment environment) {

        var configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(environment.getProperty("REDIS_HOST", "localhost"));
        configuration.setPort(environment.getProperty("REDIS_PORT", Integer.class, 6379));
        configuration.setPassword(environment.getProperty("REDIS_PASSWORD"));
        configuration.setUsername(environment.getProperty("REDIS_USERNAME"));

        return configuration;
    }


    @Bean
    LettuceConnectionFactory lettuceConnectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration) {

        return new LettuceConnectionFactory(redisStandaloneConfiguration);
    }


    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisStandaloneConfiguration redisStandaloneConfiguration) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory(redisStandaloneConfiguration));
        return template;
    }

}
