package de.avatic.lcc.config;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

//@Configuration
//@EnableWebMvc
//@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsConfig implements WebMvcConfigurer {

//    private final Environment environment;
//
//    @Value("${lcc.allowed_cors}")
//    private String allowedCors;
//
//    public CorsConfig(Environment environment) {
//        this.environment = environment;
//    }
//
//    @Override
//    public void addCorsMappings(@NotNull CorsRegistry registry) {
//        String[] activeProfiles = environment.getActiveProfiles();
//
//        System.out.println("Active profiles: " + Arrays.toString(activeProfiles));
//        System.out.println("Allowed CORS: " + allowedCors);
//
//        if (Arrays.asList(activeProfiles).contains("dev")) {
//
//            System.out.println("Applying DEV CORS configuration");
//
//            // Development CORS configuration
//            registry.addMapping("/api/**")
//                    .allowedOriginPatterns("http://localhost:*")
//                    .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                    .allowedHeaders("*")
//                    .exposedHeaders("X-Total-Count", "X-Page-Count", "X-Current-Page")
//                    .allowCredentials(true);
//
//            // OAuth endpoints
//            registry.addMapping("/oauth/**")
//                    .allowedOriginPatterns("http://localhost:*")
//                    .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                    .allowedHeaders("*")
//                    .allowCredentials(true);
//        } else {
//
//            System.out.println("Applying PROD CORS configuration");
//
//            // Production CORS configuration
//            registry.addMapping("/api/**")
//                    .allowedOrigins(allowedCors)
//                    .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                    .allowedHeaders("*")
//                    .exposedHeaders("X-Total-Count", "X-Page-Count", "X-Current-Page")
//                    .allowCredentials(true);
//
//            // OAuth endpoints
//            registry.addMapping("/oauth/**")
//                    .allowedOriginPatterns("*")
//                    .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                    .allowedHeaders("*")
//                    .allowCredentials(true);
//        }
//    }
}