package com.qqchen.deploy.backend.framework.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;

@Slf4j
public class PackageScanner {
    
    public static Set<String> scanControllerPackages(String basePackage) {
        Set<String> packages = new HashSet<>();
        
        ClassPathScanningCandidateComponentProvider scanner = 
            new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
        
        Set<BeanDefinition> components = scanner.findCandidateComponents(basePackage);
        for (BeanDefinition component : components) {
            String className = component.getBeanClassName();
            if (className != null) {
                try {
                    Class<?> clazz = Class.forName(className);
                    packages.add(clazz.getPackage().getName());
                } catch (ClassNotFoundException e) {
                    log.warn("Failed to load class: {}", className, e);
                }
            }
        }
        
        return packages;
    }
} 