package com.tashow.cloud.protection.idempotent.config;

import com.tashow.cloud.protection.idempotent.core.keyresolver.impl.ExpressionIdempotentKeyResolver;
import com.tashow.cloud.protection.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import com.tashow.cloud.protection.idempotent.core.aop.IdempotentAspect;
import com.tashow.cloud.protection.idempotent.core.keyresolver.IdempotentKeyResolver;
import com.tashow.cloud.protection.idempotent.core.keyresolver.impl.DefaultIdempotentKeyResolver;
import com.tashow.cloud.protection.idempotent.core.redis.IdempotentRedisDAO;
import com.tashow.cloud.redis.config.TashowRedisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

@AutoConfiguration(after = TashowRedisAutoConfiguration.class)
public class IdempotentConfiguration {

    @Bean
    public IdempotentAspect idempotentAspect(List<IdempotentKeyResolver> keyResolvers, IdempotentRedisDAO idempotentRedisDAO) {
        return new IdempotentAspect(keyResolvers, idempotentRedisDAO);
    }

    @Bean
    public IdempotentRedisDAO idempotentRedisDAO(StringRedisTemplate stringRedisTemplate) {
        return new IdempotentRedisDAO(stringRedisTemplate);
    }

    // ========== 各种 IdempotentKeyResolver Bean ==========

    @Bean
    public DefaultIdempotentKeyResolver defaultIdempotentKeyResolver() {
        return new DefaultIdempotentKeyResolver();
    }

    @Bean
    public UserIdempotentKeyResolver userIdempotentKeyResolver() {
        return new UserIdempotentKeyResolver();
    }

    @Bean
    public ExpressionIdempotentKeyResolver expressionIdempotentKeyResolver() {
        return new ExpressionIdempotentKeyResolver();
    }

}
