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

import com.caliverse.admin.global.common.constants.CommonConstants;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class DateUtils {
    public static String stringToDateTime(LocalDateTime date) {
        if(date == null) return null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(CommonConstants.FORMAT_DATE_DATETIME);
        return date.format(formatter);
    }

    public static String stringToISODateTime(LocalDateTime date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(CommonConstants.FORMAT_DATE_ISO_DATETIME);
        return date.atOffset(ZoneOffset.UTC).format(formatter);
    }

    public static String stringToISODateTimeMillis(LocalDateTime date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(CommonConstants.FORMAT_DATE_ISO_DATETIME_MILLIS);
        return date.atOffset(ZoneOffset.UTC).format(formatter);
    }

    public static String stringToISODateTimeMillisNano(LocalDateTime date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(CommonConstants.FORMAT_DATE_ISO_DATETIME_MILLIS_NANO);
        return date.atOffset(ZoneOffset.UTC).format(formatter);
    }

    public static LocalDateTime stringISOToLocalDateTime(String date){
        Instant instant = Instant.parse(date);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    public static LocalDateTime stringToLocalDateTime(String date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(date, formatter);
    }

    public static String nowDateTime(){
        LocalDateTime now = LocalDateTime.now();
        return stringToDateTime(now);
    }

    public static String getDynamodbDateFormat(LocalDateTime date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS");
        return date.format(formatter);
    }

    public static String getMailDateFormat(LocalDateTime date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(CommonConstants.FORMAT_DATE_MAIL_DATETIME);
        return date.atOffset(ZoneOffset.UTC).format(formatter);
    }

    public static LocalDateTime getMaxTime(){
        return LocalDateTime.of(3000, 1, 1, 0, 0, 0, 0);
    }

    public static long dateToISOUnixTime(LocalDateTime date){
        return date.atOffset(ZoneOffset.UTC).toEpochSecond();
    }
}
