package link.at17.mid.tushare.system.util;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.context.annotation.Configuration;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

public class SpringBeanDetector {

    @SuppressWarnings("rawtypes")
    private static final List<Class> SPRING_COMPONENT_ANNOTATIONS = List.of(
        Component.class,
        Service.class,
        Repository.class,
        Controller.class,
        RestController.class,
        Configuration.class,
        ControllerAdvice.class,
        RestControllerAdvice.class,
        Aspect.class
    );

    @SuppressWarnings("unchecked")
    public static boolean isSpringManagedClass(Class<?> clazz) {
        for (@SuppressWarnings("rawtypes") Class ann : SPRING_COMPONENT_ANNOTATIONS) {
            if (AnnotationUtils.findAnnotation(clazz, ann) != null) {
                return true;
            }
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    public static String findMatchedAnnotation(Class<?> clazz) {
        for (@SuppressWarnings("rawtypes") Class ann : SPRING_COMPONENT_ANNOTATIONS) {
            if (AnnotationUtils.findAnnotation(clazz, ann) != null) {
                return ann.getSimpleName();
            }
        }
        return null;
    }
}