package com.samsung.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Configuration
@EnableCaching
@Profile({"xxx"})/*@Profile({"!local"})*/
public class RedisConfig {

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private int port;

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        return RedisCacheConfiguration.defaultCacheConfig()
                .disableCachingNullValues()
                .serializeKeysWith(
                        RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())
                )
                .serializeValuesWith(
                        RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer())
                );
    }

    @Bean
    public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
        return (builder) -> builder
                .withCacheConfiguration("message-all",
                        RedisCacheConfiguration.defaultCacheConfig())
                .withCacheConfiguration("message",
                        RedisCacheConfiguration.defaultCacheConfig())
                .withCacheConfiguration("menu-all",
                        RedisCacheConfiguration.defaultCacheConfig())
                .withCacheConfiguration("menu-label-all",
                        RedisCacheConfiguration.defaultCacheConfig())
                .withCacheConfiguration("menu-full-path-all",
                        RedisCacheConfiguration.defaultCacheConfig())
                .withCacheConfiguration("page-full-path-all",
                        RedisCacheConfiguration.defaultCacheConfig())
                .withCacheConfiguration("api-full-path-all",
                        RedisCacheConfiguration.defaultCacheConfig())
                .withCacheConfiguration("api-user",
                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(300)))
                .withCacheConfiguration("api-user-menu",
                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(300)))
                .withCacheConfiguration("page-all-by-menu-auth",
                        RedisCacheConfiguration.defaultCacheConfig());
    }
}