package cn.novalon.gym.manage.app;

import cn.novalon.gym.manage.sys.core.service.IOperationLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.web.server.WebFilter;

import java.util.List;

@SpringBootApplication(scanBasePackages = "cn.novalon.gym.manage", exclude = {
        ReactiveUserDetailsServiceAutoConfiguration.class })
//@EnableScheduling
@EnableR2dbcRepositories(basePackages = {
        "cn.novalon.gym.manage.db.dao",
        "cn.novalon.gym.manage.sys.audit.repository" ,
        "cn.novalon.gym.manage.gymmembercard.dao",
        "cn.novalon.gym.manage.member.repository",
        "cn.novalon.gym.manage.groupcourse.dao",
        "cn.novalon.gym.manage.checkIn.repository"
})
@EnableReactiveElasticsearchRepositories(basePackages = "cn.novalon.gym.manage.member.es.repository")
public class ManageApplication {

    private static final Logger logger = LoggerFactory.getLogger(ManageApplication.class);

    public static void main(String[] args) {
        logger.info("应用程序启动中...");
        logger.info("包扫描路径: cn.novalon.gym.manage");

        SpringApplication.run(ManageApplication.class, args);
        logger.info("应用程序启动完成");
    }

    @Bean
    public CommandLineRunner checkWebFilters(List<WebFilter> webFilters) {
        return args -> {
            logger.info("=== 检查已注册的 WebFilter ===");
            logger.info("WebFilter 总数: {}", webFilters.size());
            for (WebFilter filter : webFilters) {
                logger.info("  - {} (Order: {})",
                        filter.getClass().getName(),
                        filter.getClass().getAnnotation(org.springframework.core.annotation.Order.class) != null
                                ? filter.getClass().getAnnotation(org.springframework.core.annotation.Order.class)
                                        .value()
                                : "无");
            }
        };
    }

    @Bean
    public CommandLineRunner checkOperationLogService(IOperationLogService service) {
        return args -> {
            logger.info("=== 检查 IOperationLogService ===");
            logger.info("IOperationLogService 实现: {}", service.getClass().getName());
        };
    }
}
