package cloud.nstart.terravault.ordinis.readapi.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 FlattenedRecordResponse(
    UUID id,
    String businessKey,
    JsonNode data,
    String geometryWkt,
    DataScope dataScope,
    OffsetDateTime validFrom,
    OffsetDateTime validTo,
    Meta _meta) {

  public record Meta(String locale, String defaultLocale, OffsetDateTime asOf) {}

  public static FlattenedRecordResponse of(
      DictionaryRecord r,
      JsonNode flattenedData,
      String chosenLocale,
      String defaultLocale,
      OffsetDateTime asOf) {
    String wkt = r.getGeometry() == null ? null : new WKTWriter().write(r.getGeometry());
    return new FlattenedRecordResponse(
        r.getId(),
        r.getBusinessKey(),
        flattenedData,
        wkt,
        r.getDataScope(),
        r.getValidFrom(),
        r.getValidTo(),
        new Meta(chosenLocale, defaultLocale, asOf));
  }
}
