import { api } from '@common/services/api';

// ============================================================
// HNS 분석 API
// ============================================================

export interface HnsAnalysisItem {
  hnsAnlysSn: number;
  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;
}

export interface CreateHnsAnalysisInput {
  anlysNm: string;
  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;
}

export async function fetchHnsAnalyses(params?: {
  status?: string;
  substance?: string;
  search?: string;
}): Promise<HnsAnalysisItem[]> {
  const response = await api.get<HnsAnalysisItem[]>('/hns/analyses', { params });
  return response.data;
}

export async function fetchHnsAnalysis(sn: number): Promise<HnsAnalysisItem> {
  const response = await api.get<HnsAnalysisItem>(`/hns/analyses/${sn}`);
  return response.data;
}

export async function createHnsAnalysis(
  input: CreateHnsAnalysisInput,
): Promise<{ hnsAnlysSn: number }> {
  const response = await api.post<{ hnsAnlysSn: number }>('/hns/analyses', input);
  return response.data;
}

export async function saveHnsAnalysis(
  sn: number,
  data: {
    rsltData: Record<string, unknown>;
    execSttsCd?: string;
    riskCd?: string;
  },
): Promise<void> {
  await api.post(`/hns/analyses/${sn}/save`, data);
}

export async function deleteHnsAnalysis(sn: number): Promise<void> {
  await api.delete(`/hns/analyses/${sn}`);
}
