package com.airlock.iam.base.infrastructure.db;

import jakarta.annotation.Nullable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public final class DbTimestamps {
   public static LocalDateTime now() {
      return ofInstant(Instant.now());
   }

   public static LocalDateTime ofInstant(Instant instant) {
      return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
   }

   @Nullable
   public static LocalDateTime ofNullableInstant(@Nullable Instant instant) {
      return instant != null ? ofInstant(instant) : null;
   }

   public static Instant toInstant(LocalDateTime localDateTime) {
      return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
   }

   @Nullable
   public static Instant toInstantFromNullable(@Nullable LocalDateTime localDateTime) {
      return localDateTime != null ? toInstant(localDateTime) : null;
   }

   private DbTimestamps() {
      throw new AssertionError();
   }
}
