/**
 * @description
 * 확산 예측 탭의 도메인 인터페이스 모음. 예측 분석·사고 상세·역추적 API
 * 응답, 시뮬레이션 입출력(바람/해류 격자·중심점·유류 입자·요약), 민감자원
 * GeoJSON, 확산 입자 GeoJSON, 이미지 분석 결과, GSC 외부 사고 목록, 그리고
 * 프런트 민감자원 모델(SensitiveResource)까지 포함한다.
 *
 * @author dnlee (dnlee@gcsc.co.kr)
 */

/** 확산 예측 — 예측 분석 목록 API 응답 (모델별 상태 포함) */
export interface PredictionAnalysis {
  acdntSn: number;
  acdntNm: string;
  occurredAt: string;
  analysisDate: string;
  requestor: string;
  duration: string;
  oilType: string;
  volume: number | null;
  location: string;
  lat: number | null;
  lon: number | null;
  kospsStatus: string;
  poseidonStatus: string;
  opendriftStatus: string;
  backtrackStatus: string;
  analyst: string;
  officeName: string;
  acdntSttsCd: string;
  predRunSn: number | null;
  runDtm: string | null;
}

/** 확산 예측 — 사고 상세 응답 (사고·유출·선박·기상) */
export interface PredictionDetail {
  acdnt: {
    acdntSn: number;
    acdntNm: string;
    occurredAt: string;
    lat: number | null;
    lon: number | null;
    location: string;
    analyst: string;
    officeName: string;
  };
  spill: {
    oilType: string;
    volume: number | null;
    unit: string;
    fcstHr: number | null;
  } | null;
  vessels: Array<{
    vesselInfoSn: number;
    imoNo: string;
    vesselNm: string;
    vesselTp: string;
    loaM: number | null;
    breadthM: number | null;
    draftM: number | null;
    gt: number | null;
    dwt: number | null;
    builtYr: number | null;
    flagCd: string;
    callsign: string;
    engineDc: string;
    insuranceData: unknown;
  }>;
  weather: Array<{
    obsDtm: string;
    locNm: string;
    temp: string;
    weatherDc: string;
    wind: string;
    wave: string;
    humid: string;
    vis: string;
    sst: string;
  }>;
}

/** 역추적 — 역추적 분석 결과 API 응답 */
export interface BacktrackResult {
  backtrackSn: number;
  acdntSn: number;
  estSpilDtm: string | null;
  anlysRange: string | null;
  lon: number | null;
  lat: number | null;
  srchRadiusNm: number | null;
  totalVessels: number | null;
  execSttsCd: string;
  rsltData: Record<string, unknown> | null;
}

/** 확산 예측 — 시뮬레이션 실행 응답 (모델별 execSn 포함) */
export interface SimulationRunResponse {
  success: boolean;
  execSn: number;
  execSns: Array<{ model: string; execSn: number }>;
  acdntSn: number | null;
  status: 'RUNNING';
}

/** 확산 예측 — 풍력 포인트 (격자 셀의 풍속/풍향) */
export interface WindPoint {
  lat: number;
  lon: number;
  wind_speed: number;
  wind_direction: number;
}

/** 확산 예측 — 해양 격자 정의 (bounds·rows·cols) */
export interface HydrGrid {
  lonInterval: number[];
  boundLonLat: { top: number; bottom: number; left: number; right: number };
  rows: number;
  cols: number;
  latInterval: number[];
}

/** 확산 예측 — 해양 흐름 격자 데이터 (u_2d/v_2d 2D 배열) */
export interface HydrDataStep {
  value: [number[][], number[][]];
  grid: HydrGrid;
}

/** 확산 예측 — 유류 중심점 궤적 (시간별 이동 경로) */
export interface CenterPoint {
  lat: number;
  lon: number;
  time: number;
  model?: string;
}

/** 확산 예측 — 개별 유류 입자 (stranded 여부 포함) */
export interface OilParticle {
  lat: number;
  lon: number;
  time: number;
  particle?: number;
  stranded?: 0 | 1;
  model?: string;
}

/** 확산 예측 — 시뮬레이션 요약 (잔존·풍화·증발·분산·해안부착량) */
export interface SimulationSummary {
  remainingVolume: number;
  weatheredVolume: number;
  evaporationVolume: number;
  dispersionVolume: number;
  pollutionArea: number;
  beachedVolume: number;
  pollutionCoastLength: number;
}

/** 확산 예측 — 시뮬레이션 상태 조회 응답 (PENDING/RUNNING/DONE/ERROR) */
export interface SimulationStatusResponse {
  status: 'PENDING' | 'RUNNING' | 'DONE' | 'ERROR';
  progress?: number;
  trajectory?: OilParticle[];
  summary?: SimulationSummary;
  centerPoints?: CenterPoint[];
  windData?: WindPoint[][];
  hydrData?: (HydrDataStep | null)[];
  error?: string;
}

/** 확산 예측 — 동기식 모델 실행 단일 결과 (모델별) */
export interface RunModelSyncResult {
  model: string;
  execSn: number;
  status: 'DONE' | 'ERROR';
  trajectory?: OilParticle[];
  summary?: SimulationSummary;
  stepSummaries?: SimulationSummary[];
  centerPoints?: CenterPoint[];
  windData?: WindPoint[][];
  hydrData?: (HydrDataStep | null)[];
  error?: string;
}

/** 확산 예측 — 동기식 다중 모델 실행 응답 래퍼 */
export interface RunModelSyncResponse {
  success: boolean;
  acdntSn: number | null;
  execSns: Array<{ model: string; execSn: number }>;
  results: RunModelSyncResult[];
}

/** 확산 예측 — 궤적 조회 응답 (모델별 데이터 맵 포함) */
export interface TrajectoryResponse {
  trajectory: OilParticle[] | null;
  summary: SimulationSummary | null;
  centerPoints?: CenterPoint[];
  windDataByModel?: Record<string, WindPoint[][]>;
  hydrDataByModel?: Record<string, (HydrDataStep | null)[]>;
  summaryByModel?: Record<string, SimulationSummary>;
  stepSummariesByModel?: Record<string, SimulationSummary[]>;
}


/** 민감자원 — 카테고리별 집계 응답 (count·totalArea) */
export interface OilSpillSummary {
  model: string;
  forecastDurationHr: number | null;
  maxSpreadDistanceKm: number | null;
  coastArrivalTimeHr: number | null;
  affectedCoastlineKm: number | null;
  weatheringRatePct: number | null;
  remainingVolumeKl: number | null;
}

export interface OilSpillSummaryResponse {
  primary: OilSpillSummary | null;
  byModel: Record<string, OilSpillSummary>;
}

export interface SensitiveResourceCategory {
  category: string;
  count: number;
  totalArea: number | null;
}

/** 민감자원 — GeoJSON Feature (민감자원 개별) */
export interface SensitiveResourceFeature {
  type: 'Feature';
  geometry: { type: string; coordinates: unknown };
  properties: {
    srId: number;
    category: string;
    [key: string]: unknown;
  };
}

/** 민감자원 — GeoJSON FeatureCollection (민감자원 전체) */
export interface SensitiveResourceFeatureCollection {
  type: 'FeatureCollection';
  features: SensitiveResourceFeature[];
}

/** 확산 예측 — 확산 입자 GeoJSON (시간별 isLastStep 플래그 포함) */
export interface SpreadParticlesGeojson {
  type: 'FeatureCollection';
  features: Array<{
    type: 'Feature';
    geometry: { type: 'Point'; coordinates: [number, number] };
    properties: { model: string; time: number; stranded: 0 | 1; isLastStep: boolean };
  }>;
  maxStep: number;
}

/** 이미지 분석 — 항공사진 업로드 분석 결과 (area·volume·fileId) */
export interface ImageAnalyzeResult {
  acdntSn: number;
  lat: number;
  lon: number;
  oilType: string;
  area: number;
  volume: number;
  fileId: string;
  occurredAt: string;
}

/** GSC 연계 — 외부 수집 사고 목록 (확산 예측 입력 셀렉트용) */
export interface GscAccidentListItem {
  acdntSn: number;
  acdntMngNo: string;
  pollNm: string;
  pollDate: string | null;
  lat: number | null;
  lon: number | null;
}

/** 민감자원 — 프런트 민감자원 모델 (양식장/해수욕장/생태/취수구) */
export interface SensitiveResource {
  id: string;
  name: string;
  type: 'aquaculture' | 'beach' | 'ecology' | 'intake';
  lat: number;
  lon: number;
  radiusM: number;
  arrivalTimeH: number;
}
