package com.eactive.testmaster.config;

/**
 * Compatible with Java 8+
 * Required dependencies:
 * - com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.0+ (for Java 8)
 * - com.fasterxml.jackson.core:jackson-databind:2.9.0+
 */

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

//@Configuration
//public class DateTimeConfig {
//
//    private static final String ISO_DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
//
//    @Bean
//    public ObjectMapper objectMapper() {
//        JavaTimeModule module = new JavaTimeModule();
//        LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(
//            DateTimeFormatter.ofPattern(ISO_DATE_TIME_PATTERN)
//        );
//        LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(
//            DateTimeFormatter.ofPattern(ISO_DATE_TIME_PATTERN)
//        );
//
//        module.addSerializer(LocalDateTime.class, localDateTimeSerializer);
//        module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
//
//        return Jackson2ObjectMapperBuilder.json()
//            .modules(module)
//            .build();
//    }
//}

//@Configuration
//public class DateTimeConfig {
//    private static final String ISO_DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
//
//    @Bean
//    public ObjectMapper objectMapper() {
//        ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
//            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)  // This is key!
//            .modules(new JavaTimeModule())
//            .build();
//
//        // Configure specific DateTime format if needed
//        mapper.setDateFormat(new SimpleDateFormat(ISO_DATE_TIME_PATTERN));
//
//        return mapper;
//    }
//}

@Configuration
public class DateTimeConfig {
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .modules(new JavaTimeModule())
            .serializationInclusion(JsonInclude.Include.NON_NULL)
            .dateFormat(new StdDateFormat())
            .build();

        // Configure LocalDateTime serialization
        SimpleModule module = new SimpleModule();
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(
            DateTimeFormatter.ISO_DATE_TIME));
        mapper.registerModule(module);

        return mapper;
    }
}
