package cloud.nstart.terravault.ordinis.events;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.time.OffsetDateTime;
import java.util.UUID;

/**
 * Метаданные-обёртка вокруг payload каждого события. Едет в Kafka как value.
 *
 * <p>Schema versioning: {@code schemaVersion} = semver shared DTO модуля
 * (ordinis-events-api). Consumer проверяет совместимость через major version.
 *
 * @param <T> тип payload (DictionaryRecordEvent, DictionaryDefinitionEvent)
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public record EventEnvelope<T>(
    UUID eventId,
    OffsetDateTime occurredAt,
    String schemaVersion,
    String bundle,
    String dictionaryName,
    DataScope dataScope,
    String traceId,
    String spanId,
    String causedBy,
    T payload) {

  public static <T> EventEnvelope<T> of(
      String dictionaryName,
      DataScope scope,
      String bundle,
      T payload) {
    return new EventEnvelope<>(
        UUID.randomUUID(),
        OffsetDateTime.now(),
        EventSchemaVersion.CURRENT,
        bundle,
        dictionaryName,
        scope,
        null,
        null,
        null,
        payload);
  }
}
