package org.brapi.test.BrAPITestServer.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class BrApiWebConfig implements WebMvcConfigurer {

    private static final String BRAPI_PREFIX = "/brapi/v2";
    private static final String BRAPI_CONTROLLER_PACKAGE = "org.brapi.test.BrAPITestServer.controller";

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix(BRAPI_PREFIX, BrApiWebConfig::isBrApiController);
    }

    static boolean isBrApiController(Class<?> controllerClass) {
        return controllerClass.isAnnotationPresent(RestController.class)
                && controllerClass.getPackageName().startsWith(BRAPI_CONTROLLER_PACKAGE);
    }
}
