package gc.mda.signal_batch.global.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import gc.mda.signal_batch.global.util.DateTimeParseUtil;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 다양한 날짜/시간 형식을 지원하는 Jackson Deserializer
 *
 * <p>지원 형식:</p>
 * <ul>
 *   <li>ISO 8601: 2026-01-20T10:00:00</li>
 *   <li>ISO 8601 (UTC): 2026-01-20T10:00:00Z</li>
 *   <li>ISO 8601 (Offset): 2026-01-20T10:00:00+09:00</li>
 *   <li>표준 형식: 2026-01-20 10:00:00</li>
 *   <li>초 생략: 2026-01-20T10:00, 2026-01-20 10:00</li>
 *   <li>압축 형식: 20260120100000</li>
 * </ul>
 *
 * <p>사용 예:</p>
 * <pre>{@code
 * @JsonDeserialize(using = FlexibleLocalDateTimeDeserializer.class)
 * private LocalDateTime startTime;
 * }</pre>
 *
 * @see DateTimeParseUtil
 */
public class FlexibleLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String value = p.getValueAsString();
        if (value == null || value.isBlank()) {
            return null;
        }

        try {
            return DateTimeParseUtil.parse(value);
        } catch (IllegalArgumentException e) {
            throw new IOException("날짜/시간 파싱 실패: " + value + ". " + e.getMessage(), e);
        }
    }
}
