/**
 * @description
 * HNS(유해화학물질) 대기확산 탭의 도메인 인터페이스 모음. HNS 분석 API
 * 응답·요청, Gaussian 확산 계산 입력(기상·누출원·격자)과 결과(히트맵·AEGL
 * 거리·면적·등농도선), 물질 독성·기상 조회 결과, 시나리오·재계산 파라미터,
 * 물질 검색 DB(화물적부도)까지 전체 도메인 타입을 담는다.
 *
 * @author dnlee (dnlee@gcsc.co.kr)
 */
import type {
  StabilityClass,
  DispersionModel,
  AlgorithmType,
  ReleaseType,
  Severity,
} from '@/types/hns/HnsType';

/** HNS 분석 — 분석 목록 API 응답 아이템 */
export interface HnsAnalysisItem {
  hnsAnlysSn: number;
  acdntSn: number | null;
  anlysNm: string;
  acdntDtm: string | null;
  locNm: string | null;
  lon: number | null;
  lat: number | null;
  sbstNm: string | null;
  spilQty: number | null;
  spilUnitCd: string | null;
  fcstHr: number | null;
  algoCd: string | null;
  critMdlCd: string | null;
  windSpd: number | null;
  windDir: string | null;
  execSttsCd: string;
  riskCd: string | null;
  analystNm: string | null;
  rsltData: Record<string, unknown> | null;
  regDtm: string;
}

/** HNS 분석 — 분석 생성 요청 페이로드 */
export interface CreateHnsAnalysisInput {
  anlysNm: string;
  acdntSn?: number;
  acdntDtm?: string;
  locNm?: string;
  lon?: number;
  lat?: number;
  sbstNm?: string;
  spilQty?: number;
  spilUnitCd?: string;
  fcstHr?: number;
  algoCd?: string;
  critMdlCd?: string;
  windSpd?: number;
  windDir?: string;
  temp?: number;
  humid?: number;
  atmStblCd?: string;
  analystNm?: string;
}

/** HNS 확산 — 기상 조건 입력 (풍속·안정도·혼합층) */
export interface MeteoParams {
  windSpeed: number;
  windDirDeg: number;
  stability: StabilityClass;
  temperature: number;
  pressure: number;
  mixingHeight: number;
}

/** HNS 확산 — 누출원 정보 입력 (배출률·총량·위치·분자량) */
export interface SourceParams {
  Q: number;
  QTotal: number;
  x0: number;
  y0: number;
  z0: number;
  releaseDuration: number;
  molecularWeight: number;
  vaporPressure: number;
  densityGas: number;
  poolRadius: number;
}

/** HNS 확산 — 시뮬레이션 격자·시간 설정 */
export interface SimParams {
  xRange: [number, number];
  yRange: [number, number];
  nx: number;
  ny: number;
  zRef: number;
  tStart: number;
  tEnd: number;
  dt: number;
}

/** HNS 확산 — 지도 위 농도 점 (HeatmapLayer 입력) */
export interface DispersionPoint {
  lon: number;
  lat: number;
  concentration: number;
}

/** HNS 확산 — AEGL 3단계 풍하 최대 도달 거리(m) */
export interface AeglDistances {
  aegl1: number;
  aegl2: number;
  aegl3: number;
}

/** HNS 확산 — AEGL 3단계 피해 면적(km²) */
export interface AeglAreas {
  aegl1: number;
  aegl2: number;
  aegl3: number;
}

/** HNS 확산 — AEGL 등농도선 (marching squares 결과) */
export interface ContourLine {
  level: 'AEGL-1' | 'AEGL-2' | 'AEGL-3';
  threshold: number;
  color: string;
  segments: Array<[[number, number], [number, number]]>;
}

/** HNS 확산 — 확산 계산 전체 결과 (농도·AEGL·등농도선 포함) */
export interface DispersionGridResult {
  points: DispersionPoint[];
  maxConcentration: number;
  aeglDistances: AeglDistances;
  aeglAreas: AeglAreas;
  modelType: DispersionModel;
  timeStep: number;
  substance: string;
  contours?: ContourLine[];
}

/** HNS 확산 — computeDispersion 함수 입력 파라미터 */
export interface ComputeDispersionParams {
  meteo: MeteoParams;
  source: SourceParams;
  sim: SimParams;
  modelType: DispersionModel;
  originLon: number;
  originLat: number;
  substanceName: string;
  t: number;
  algorithm?: AlgorithmType;
}

/** HNS 확산 — 물질 독성 정보 (AEGL·IDLH·물리상수) */
export interface SubstanceToxicity {
  nameKr: string;
  nameEn: string;
  aegl1: number;
  aegl2: number;
  aegl3: number;
  idlh: number;
  mw: number;
  densityGas: number;
  vaporPressure: number;
  poolRadius: number;
  Q: number;
  QTotal: number;
}

/** HNS 확산 — 기상 자동조회 결과 (실시간 관측 + 안정도 추정) */
export interface WeatherFetchResult {
  windSpeed: number;
  windDirection: number;
  temperature: number;
  humidity: number;
  stability: StabilityClass;
  isLoading: boolean;
  error: string | null;
  lastUpdate: Date | null;
}

/** HNS 분석 — 좌측 패널 입력 파라미터 (물질·유출형·기상 조합) */
export interface HNSInputParams {
  substance: string;
  releaseType: ReleaseType;
  emissionRate: number;
  totalRelease: number;
  releaseHeight: number;
  releaseDuration: number;
  poolRadius: number;
  algorithm: string;
  criteriaModel: string;
  weather: WeatherFetchResult;
  accidentDate: string;
  accidentTime: string;
  predictionTime: string;
  accidentName: string;
  /** wing.ACDNT 사고번호 (사고 리스트에서 선택된 경우) */
  selectedAcdntSn?: number;
}

/** HNS 분석 — 재계산 모달 입력 파라미터 */
export interface RecalcParams {
  substance: string;
  releaseType: ReleaseType;
  emissionRate: number;
  totalRelease: number;
  algorithm: string;
  predictionTime: string;
}

/** HNS 시나리오 — 시간 단계별 시나리오 UI 표시 모델 */
export interface HnsScenario {
  id: string;
  name: string;
  severity: Severity;
  timeStep: string;
  datetime: string;
  wind: string;
  maxConc: string;
  idlhRadius: string;
  erpg2: string;
  population: string;
  description: string;
  detail: {
    maxConc: string;
    idlhRadius: string;
    erpg2: string;
    windDir: string;
    windSpeed: string;
    population: string;
    spillAmount: string;
  };
  zones: { idlh: string; erpg2: string; erpg1: string; twa: string };
  weather: {
    dir: string;
    speed: string;
    temp: string;
    stability: string;
    humidity: string;
    mixHeight: string;
  };
  actions: string[];
}

/** HNS 시나리오 — 물질 프로퍼티 표시용 카드 모델 */
export interface HnsMaterial {
  key: string;
  name: string;
  mw: string;
  bp: string;
  fp: string;
  idlh: string;
  erpg2: string;
}

/** HNS 물질 검색 — 화물적부도 기반 물질 DB (물리/독성/MSDS/IBC/항구별 반입) */
export interface HNSSearchSubstance {
  id: number;
  abbreviation: string;
  nameKr: string;
  nameEn: string;
  synonymsEn: string;
  synonymsKr: string;
  unNumber: string;
  casNumber: string;
  transportMethod: string;
  sebc: string;
  usage: string;
  state: string;
  color: string;
  odor: string;
  flashPoint: string;
  autoIgnition: string;
  boilingPoint: string;
  density: string;
  solubility: string;
  vaporPressure: string;
  vaporDensity: string;
  explosionRange: string;
  nfpa: { health: number; fire: number; reactivity: number; special: string };
  hazardClass: string;
  ergNumber: string;
  idlh: string;
  aegl2: string;
  erpg2: string;
  responseDistanceFire: string;
  responseDistanceSpillDay: string;
  responseDistanceSpillNight: string;
  marineResponse: string;
  ppeClose: string;
  ppeFar: string;
  msds: {
    hazard: string;
    firstAid: string;
    fireFighting: string;
    spillResponse: string;
    exposure: string;
    regulation: string;
  };
  ibcHazard: string;
  ibcShipType: string;
  ibcTankType: string;
  ibcDetection: string;
  ibcFireFighting: string;
  ibcMinRequirement: string;
  emsCode: string;
  emsFire: string;
  emsSpill: string;
  emsFirstAid: string;
  cargoCodes: Array<{ code: string; name: string; company: string; source: string }>;
  portFrequency: Array<{ port: string; portCode: string; lastImport: string; frequency: string }>;
}
