package com.airlock.iam.common.api.domain.model.event;

import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import jakarta.annotation.Nullable;
import java.util.Objects;
import java.util.Set;
import lombok.NonNull;

public interface ContextDataChanged extends UserEvent {
   ELogger LOG = ELoggerFactory.getLogger(ContextDataChanged.class);

   Set<ContextDataChanged.ContextDataChange> changes();

   record ContextDataChange(@NonNull String name, @Nullable Object oldValue, @Nullable Object newValue) {
      public ContextDataChange {
         if (name == null) {
            throw new NullPointerException("name is marked non-null but is null");
         }

         if (Objects.equals(oldValue, newValue)) {
            ContextDataChanged.LOG.debug("Old and new value for context data field '%s' must not be equal ('%s') in a change event.".formatted(name, newValue));
            throw new IllegalArgumentException("Old and new value for context data field '%s' must not be equal in a change event.".formatted(name));
         }
      }
   }
}
