package br.gov.sigefp.api.config;

import br.gov.sigefp.common.util.GuineaBissauConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.format.DateTimeFormatter;

/**
 * Configuração web (CORS, formatação, etc.) para Guiné-Bissau.
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOrigins("*") // TODO: Configurar origens permitidas em produção
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        
        // Configurar formatos de data e hora para Guiné-Bissau
        registrar.setDateFormatter(DateTimeFormatter.ofPattern(GuineaBissauConfig.DATE_FORMAT, GuineaBissauConfig.LOCALE));
        registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HH:mm", GuineaBissauConfig.LOCALE));
        registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern(GuineaBissauConfig.DATETIME_FORMAT, GuineaBissauConfig.LOCALE));
        
        registrar.registerFormatters(registry);
    }
}

