package com.red.circle.other.app.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 火箭异步任务配置
 *
 * @author system
 * @date 2025-01-15
 */
@Slf4j
@Configuration
@EnableAsync
@EnableScheduling
public class RocketAsyncConfig {

    /**
     * 火箭发射专用线程池
     * 并发场景：100个活跃房间，活动期间可能有20-30个房间同时触发发射
     */
    @Bean("rocketLaunchExecutor")
    public Executor rocketLaunchExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 核心线程数
        executor.setCorePoolSize(10);
        
        // 最大线程数
        executor.setMaxPoolSize(30);
        
        // 队列容量
        executor.setQueueCapacity(50);
        
        // 线程名前缀
        executor.setThreadNamePrefix("rocket-launch-");
        
        // 拒绝策略：发射任务重要，使用调用者运行策略保证执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        // 线程空闲时间（秒）
        executor.setKeepAliveSeconds(60);
        
        // 关闭时等待任务完成
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        
        executor.initialize();
        
        log.info("火箭发射线程池初始化完成: core={}, max={}, queue={}", 
                executor.getCorePoolSize(), executor.getMaxPoolSize(), executor.getQueueCapacity());
        
        return executor;
    }

    /**
     * 火箭领奖专用线程池
     * 使用场景：用户进房自动领奖
     */
    @Bean("rocketRewardExecutor")
    public Executor rocketRewardExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 线程数多一些，应对200-500活跃用户
        executor.setCorePoolSize(15);
        
        // 最大线程数
        executor.setMaxPoolSize(30);
        
        // 大容量队列，缓冲进房峰值请求
        executor.setQueueCapacity(500);
        
        // 线程名前缀
        executor.setThreadNamePrefix("rocket-reward-");
        
        // 拒绝策略：领奖可以丢弃，避免阻塞进房接口
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
        
        // 线程空闲时间（秒）
        executor.setKeepAliveSeconds(60);
        
        // 关闭时等待任务完成
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        
        executor.initialize();
        
        log.info("火箭领奖线程池初始化完成: core={}, max={}, queue={}", 
                executor.getCorePoolSize(), executor.getMaxPoolSize(), executor.getQueueCapacity());
        
        return executor;
    }

    /**
     * 红包异步处理专用线程池
     * 场景：抢红包后的异步写库和钱包入账
     */
    @Bean("redPacketAsyncExecutor")
    public Executor redPacketAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 核心线程数
        executor.setCorePoolSize(8);

        // 最大线程数
        executor.setMaxPoolSize(20);

        // 队列容量
        executor.setQueueCapacity(200);

        // 线程名前缀
        executor.setThreadNamePrefix("red-packet-async-");

        // 拒绝策略：重要任务，使用调用者运行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        // 线程空闲时间
        executor.setKeepAliveSeconds(60);

        // 优雅关闭
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);

        executor.initialize();

        log.info("红包异步线程池初始化完成: core={}, max={}, queue={}",
                executor.getCorePoolSize(), executor.getMaxPoolSize(), executor.getQueueCapacity());

        return executor;
    }

}
