package com.zksy.data.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.List;

public class CustomDateDeserializer extends StdDeserializer<LocalDate> {

    private final List<DateTimeFormatter> formatters;

    public CustomDateDeserializer(List<DateTimeFormatter> formatters) {
        super(LocalDate.class);
        this.formatters = formatters;
    }

    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);
        String dateStr = node.asText();

        for (DateTimeFormatter formatter : formatters) {
            try {
                return LocalDate.parse(dateStr, formatter);
            } catch (DateTimeParseException e) {
                // Try the next formatter
            }
        }

        throw new DateTimeParseException("Unsupported date format", dateStr, 0);
    }
}