package com.ffii.lioner.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
   
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**") // Apply to all API endpoints
          .allowedHeaders("*")
          // **** CRITICAL FIX HERE ****
          .allowedOrigins(
            "http://localhost",       // If you test locally via Nginx at http://localhost
            "http://127.0.0.1",       // Sometimes browsers resolve localhost to 127.0.0.1
            "http://10.40.0.4",
            "http://20.2.170.164",
            "https://20.2.170.164",
            "http://localhost:3000",
            "https://forms.lioner.com"
            // Add any other specific domains/IPs/ports where your frontend will be hosted
        )
        .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS") // **** IMPORTANT: Add OPTIONS ****
        .allowCredentials(true)
        .maxAge(3600); // Recommended: Caches preflight results for 1 hour
}

  @Bean
  public InternalResourceViewResolver defaultViewResolver() {
    return new InternalResourceViewResolver();
  }

}