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

import cloud.nstart.terravault.ordinis.domain.DataScope;
import cloud.nstart.terravault.ordinis.domain.record.DictionaryRecord;
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 RecordResponse(
    UUID id,
    UUID dictionaryId,
    String businessKey,
    JsonNode data,
    String geometryWkt,
    DataScope dataScope,
    OffsetDateTime validFrom,
    OffsetDateTime validTo,
    OffsetDateTime createdAt,
    OffsetDateTime updatedAt,
    String createdBy,
    String updatedBy,
    Long version) {

  public static RecordResponse from(DictionaryRecord r) {
    String wkt = r.getGeometry() == null ? null : new WKTWriter().write(r.getGeometry());
    return new RecordResponse(
        r.getId(),
        r.getDictionaryId(),
        r.getBusinessKey(),
        r.getData(),
        wkt,
        r.getDataScope(),
        r.getValidFrom(),
        r.getValidTo(),
        r.getCreatedAt(),
        r.getUpdatedAt(),
        r.getCreatedBy(),
        r.getUpdatedBy(),
        r.getVersion());
  }
}
