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

import cloud.nstart.terravault.ordinis.domain.DataScope;
import cloud.nstart.terravault.ordinis.domain.definition.DictionaryDefinition;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;

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

@JsonInclude(JsonInclude.Include.NON_NULL)
public record DictionaryResponse(
    UUID id,
    String name,
    String displayName,
    String description,
    DataScope scope,
    JsonNode schemaJson,
    String schemaVersion,
    String bundle,
    List<String> supportedLocales,
    String defaultLocale,
    /** CEO plan E2: per-dict Redis projection opt-in. */
    boolean redisProjectionEnabled,
    /** Approval Workflow v2: per-dict opt-in (D1=A). */
    boolean approvalRequired,
    String approvalMinRole,
    Long recordCount,
    OffsetDateTime createdAt,
    OffsetDateTime updatedAt,
    String createdBy,
    String updatedBy) {

  public static DictionaryResponse from(DictionaryDefinition d) {
    return from(d, null);
  }

  public static DictionaryResponse from(DictionaryDefinition d, Long recordCount) {
    return new DictionaryResponse(
        d.getId(),
        d.getName(),
        d.getDisplayName(),
        d.getDescription(),
        d.getScope(),
        d.getSchemaJson(),
        d.getSchemaVersion(),
        d.getBundle(),
        d.getSupportedLocales() == null ? List.of() : List.of(d.getSupportedLocales()),
        d.getDefaultLocale(),
        d.isRedisProjectionEnabled(),
        d.isApprovalRequired(),
        d.getApprovalMinRole(),
        recordCount,
        d.getCreatedAt(),
        d.getUpdatedAt(),
        d.getCreatedBy(),
        d.getUpdatedBy());
  }
}
