package cloud.nstart.terravault.ordinis.restapi.dto.schemaworkflow;

import cloud.nstart.terravault.ordinis.domain.schemaworkflow.DictionarySchemaDraft;
import cloud.nstart.terravault.ordinis.domain.schemaworkflow.SchemaDraftStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;

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

/**
 * API representation для {@link DictionarySchemaDraft}. Status returned
 * lowercase string ({@code "draft"}, {@code "review_pending"} etc) чтобы
 * match'ило API spec из docs/integration/api/pending-endpoints.md §3.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public record SchemaDraftResponse(
    UUID draftId,
    UUID dictionaryId,
    String dictionaryName,
    String status,
    String branchedFromVersion,
    JsonNode proposedSchema,
    String reason,
    String makerId,
    OffsetDateTime createdAt,
    OffsetDateTime submittedAt,
    JsonNode reviewers,
    String reviewNote,
    String decisionUserId,
    OffsetDateTime decisionAt,
    String decisionComment,
    OffsetDateTime publishedAt,
    String publishedVersion,
    String publishNote,
    Long version) {

  public static SchemaDraftResponse from(DictionarySchemaDraft d, String dictionaryName) {
    return new SchemaDraftResponse(
        d.getId(),
        d.getDictionaryId(),
        dictionaryName,
        d.getStatus().dbValue(),
        d.getBranchedFromVersion(),
        d.getProposedSchema(),
        d.getReason(),
        d.getMakerId(),
        d.getCreatedAt(),
        d.getSubmittedAt(),
        d.getReviewerAssignments(),
        d.getReviewNote(),
        d.getDecisionUserId(),
        d.getDecisionAt(),
        d.getDecisionComment(),
        d.getPublishedAt(),
        d.getPublishedVersion(),
        d.getPublishNote(),
        d.getVersion());
  }

  /** Convenience overload — dictionary name can be NULL if not yet resolved. */
  public static SchemaDraftResponse from(DictionarySchemaDraft d) {
    return from(d, null);
  }

  public SchemaDraftStatus statusEnum() {
    return SchemaDraftStatus.fromDbValue(status);
  }
}
