package com.seer.teach.common.cache.jetcache.config;

import com.alicp.jetcache.anno.config.EnableMethodCache;
import com.alicp.jetcache.autoconfigure.JetCacheAutoConfiguration;
import com.alicp.jetcache.support.Fastjson2ValueDecoder;
import com.alicp.jetcache.support.Fastjson2ValueEncoder;
import com.seer.teach.common.cache.support.YamlPropertySourceFactory;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Slf4j
@EnableMethodCache(basePackages = "com.seer")
@ConditionalOnProperty(name = "jetcache.enable", havingValue = "true", matchIfMissing = true)
@PropertySource(
        value = "classpath:jetcache.yaml",
        factory = YamlPropertySourceFactory.class
)
@RequiredArgsConstructor
@Configuration
public class JetCacheConfig  extends JetCacheAutoConfiguration {

    @PostConstruct
    public void init() {
        log.info("JetCacheConfig configuration loaded successfully");
        log.info("Enabled method cache for base packages: com.seer");
        log.info("Loading jetcache configuration from: classpath:jetcache.yaml");
    }

    @Bean
    public Fastjson2ValueDecoder fastjson2ValueDecoder() {
        return new Fastjson2ValueDecoder(false);
    }

    @Bean
    public Fastjson2ValueEncoder fastjson2ValueEncoder() {
        return new Fastjson2ValueEncoder(false);
    }
}