package com.caliverse.admin.global.common.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
@Slf4j
public class DataHelper {
    public static LocalDateTime parseDateTime(Object dateObj) {
        if (dateObj == null || dateObj.toString().isEmpty()) {
            return null;
        }
        try {
            return LocalDateTime.parse(dateObj.toString().replace("+00:00", ""));
        } catch (Exception e) {
            log.warn("Failed to parse date: {}", dateObj);
            return null;
        }
    }

    public static Float parseFloat(Object floatObj) {
        if (floatObj == null) {
            return null;
        }
        if (floatObj instanceof Number) {
            return ((Number) floatObj).floatValue();
        }
        try {
            return Float.parseFloat(floatObj.toString());
        } catch (NumberFormatException e) {
            log.warn("Failed to parse float: {}", floatObj);
            return null;
        }
    }

    public static Double convertToDouble(Object value) {
        if (value == null) return null;
        if (value instanceof Double) return (Double) value;
        if (value instanceof Integer) return ((Integer) value).doubleValue();
        if (value instanceof Long) return ((Long) value).doubleValue();
        if (value instanceof String) {
            try {
                return Double.valueOf((String) value);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }
}
