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

import cloud.nstart.terravault.ordinis.domain.draft.DraftOperation;
import cloud.nstart.terravault.ordinis.domain.draft.DraftStatus;
import cloud.nstart.terravault.ordinis.domain.draft.RecordDraft;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import org.locationtech.jts.io.WKTWriter;

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

@JsonInclude(JsonInclude.Include.NON_NULL)
public record DraftResponse(
    UUID id,
    UUID dictionaryId,
    String businessKey,
    UUID parentRecordId,
    DraftOperation operation,
    JsonNode data,
    String geometryWkt,
    OffsetDateTime proposedValidFrom,
    OffsetDateTime proposedValidTo,
    DraftStatus status,
    String makerId,
    String makerComment,
    OffsetDateTime submittedAt,
    String reviewerId,
    OffsetDateTime reviewedAt,
    String reviewComment) {

  public static DraftResponse from(RecordDraft d) {
    String wkt = d.getGeometry() == null ? null : new WKTWriter().write(d.getGeometry());
    return new DraftResponse(
        d.getId(),
        d.getDictionaryId(),
        d.getBusinessKey(),
        d.getParentRecordId(),
        d.getOperation(),
        d.getData(),
        wkt,
        d.getProposedValidFrom(),
        d.getProposedValidTo(),
        d.getStatus(),
        d.getMakerId(),
        d.getMakerComment(),
        d.getSubmittedAt(),
        d.getReviewerId(),
        d.getReviewedAt(),
        d.getReviewComment());
  }
}
