// NSPD (НСПД) types — Issue #202
// Shapes derived from backend/app/services/site_finder/quarter_dump_lookup.py
// and backend/app/schemas/parcel.py

export interface NspdZoning {
  zone_code: string | null;
  zone_name: string | null;
  source?: string;
  raw_props?: Record<string, unknown>;
}

export interface NspdZouitOverlap {
  group_key: string; // e.g. "engineering", "okn", "natural", "protected", "other", "cad_zouit"
  layer: string; // e.g. "zouit_engineering"; для cad_zouit fallback = type_zone
  subcategory: string | number | null;
  name: string | null;
  // cad_zouit fallback path (quarter_dump_lookup._get_cad_zouit_overlaps) — плоские
  // поля поверх raw_props. type_zone = категория зоны (охранная зона тепла / СЗЗ / …),
  // source = "cad_zouit". В nspd-dump path обоих может не быть → optional.
  type_zone?: string | null;
  category_name?: string | null;
  source?: string;
  // #255 — geometry охранной зоны для map-слоя. ST_AsGeoJSON(geom::geometry) →
  // строка GeoJSON (Polygon/MultiPolygon). Optional + graceful: backend пока может
  // не отдавать поле (см. ZouitLayer — overlap без geom_geojson просто не рисуется).
  geom_geojson?: string | null;
  raw_props?: Record<string, unknown>;
}

export interface NspdEngineeringNearby {
  name: string | null;
  type: string | null;
  distance_m: number | null;
  raw_props?: Record<string, unknown>;
}

export interface NspdDumpMeta {
  available: boolean;
  fetched_at_utc: string | null; // ISO datetime
  stale: boolean;
  harvest_triggered: boolean;
  total_features: number | null;
  // Issue #234: typical harvest duration. Используется UI для countdown +
  // auto-stop re-poll после ETA*1.5. null когда dump уже available или harvest
  // не запущен (lock уже взят другим запросом).
  harvest_eta_seconds?: number | null;
}

// Risk zones (issue #94 TIER 3) — from /analyze nspd_risk_zones field

export interface RiskZone {
  layer: string; // e.g. "risk_flooding", "risk_landslide"
  subtype: string | null; // human-readable label from NSPD properties or mapping
  geom_wkt: string | null;
  intersection_area_sqm: number | null;
}

// Opportunity parcels (issue #94 TIER 4)

export interface OpportunityParcel {
  layer:
    | "auction_parcels"
    | "scheme_parcels"
    | "free_parcels"
    | "future_parcels"
    | "oopt";
  cad_num: string | null;
  distance_m: number | null;
  geom_wkt: string | null;
}

// Red lines (issue #94 TIER 4 + #54 Generative foundation)

export interface RedLine {
  geom_wkt: string | null;
  intersection_length_m: number | null; // null if only nearby, not intersecting
  distance_m: number | null; // null if intersecting
}

// Connection-points endpoint shapes (/api/v1/parcels/{cad}/connection-points)

export interface EngineeringStructure {
  name: string | null;
  type: string | null;
  cad_num: string | null;
  distance_to_boundary_m: number;
  geometry_geojson: Record<string, unknown>;
  readable_address: string | null;
  raw_props: Record<string, unknown>;
  source: string;
}

export interface ZouitEngineeringOverlap {
  reg_numb_border: string | null;
  type_zone: string | null;
  subcategory: number | null;
  intersects_parcel: boolean;
  geometry_geojson: Record<string, unknown>;
  raw_props: Record<string, unknown>;
  source: string;
}

export interface ConnectionPointsSummary {
  nearest_structure_distance_m: number | null;
  in_protection_zone: boolean;
  protection_zones_intersecting: number;
  total_structures_in_radius: number;
}

export interface ConnectionPointsResponse {
  engineering_structures: EngineeringStructure[];
  zouit_engineering_overlaps: ZouitEngineeringOverlap[];
  summary: ConnectionPointsSummary;
  dump_available: boolean;
  dump_fetched_at: string | null;
}
