package cloud.nstart.terravault.ordinis.notifications.event;

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

/**
 * Canonical notification event published в Kafka topic {@code ordinis.notifications.v1}
 * через existing {@code OutboxRecorder}. Consumer ({@code NotificationDispatcher})
 * deserialize'ит и fan-out'ит per recipient × channel.
 *
 * <p>Three event types:
 * <ul>
 *   <li>{@code draft.submitted} — DraftService.submit() success → notify reviewer pool.</li>
 *   <li>{@code draft.decision} — approve/reject success → notify maker.</li>
 *   <li>{@code draft.withdrawn} — withdraw success → notify reviewer pool (clear queue).</li>
 * </ul>
 *
 * <p>Schema versioned via {@code v1} suffix в topic name. Future schema changes —
 * new topic {@code v2}, dual-publish during migration.
 *
 * @param eventId UUID of the originating outbox event (idempotency key)
 * @param eventType wire string ('draft.submitted' / 'draft.decision' / 'draft.withdrawn')
 * @param occurredAt timestamp
 * @param draft draft context (always present для draft.* events)
 * @param decision present только для 'draft.decision' (APPROVE/REJECT + decidedBy + comment)
 * @param context routing hints — approvalMinRole, dictDisplayName ru/en
 */
public record NotificationEvent(
    UUID eventId,
    String eventType,
    OffsetDateTime occurredAt,
    DraftRef draft,
    Decision decision,
    Map<String, Object> context
) {

  public static final String TYPE_SUBMITTED = "draft.submitted";
  public static final String TYPE_DECISION = "draft.decision";
  public static final String TYPE_WITHDRAWN = "draft.withdrawn";

  public record DraftRef(
      UUID id,
      String businessKey,
      String dictionary,
      String operation,
      String maker
  ) {}

  public record Decision(
      String outcome,    // 'APPROVE' | 'REJECT'
      String decidedBy,
      String comment
  ) {}
}
