package com.kamco.cd.kamcoback.Innopam.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GeoJsonLoader {

  private final ObjectMapper om = new ObjectMapper();

  public GeoJsonFile load(File geoJsonFile) throws Exception {

    JsonNode root = om.readTree(geoJsonFile);

    long mapId = root.path("properties").path("map_id").asLong(-1);
    if (mapId <= 0) {
      throw new IllegalStateException(
          "GeoJSON top-level properties.map_id 없음: " + geoJsonFile.getName());
    }

    List<JsonNode> features = new ArrayList<>();
    root.path("features").forEach(features::add);

    return new GeoJsonFile(mapId, features);
  }

  /** ✅ feature에서 polygon_id 추출 */
  public static String polygonId(JsonNode feature) {
    return feature.path("properties").path("polygon_id").asText(null);
  }

  public static class GeoJsonFile {

    public final long mapId;
    public final List<JsonNode> features;

    public GeoJsonFile(long mapId, List<JsonNode> features) {
      this.mapId = mapId;
      this.features = features;
    }
  }
}
