import { normalizeResponseData, requestJson } from "./request"

const OPEN_API_BASE = import.meta.env.VITE_OPEN_API_BASE || "/openApi"

export async function generateEngineeringAdvice(statistics = {}) {
  const response = await requestJson(`${OPEN_API_BASE}/ai/ecological-engineering/advice`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      dataJson: JSON.stringify({
        engineeringName: statistics.engineeringName,
        scopeName: statistics.scopeName,
        dimensionLabel: statistics.dimensionLabel,
        baseCount: statistics.baseCount,
        excludedCount: statistics.excludedCount,
        finalCount: statistics.finalCount,
        finalAreaMu: statistics.finalAreaMu,
        projectDataLatestYear: statistics.projectDataLatestYear,
        filters: statistics.filters,
        distribution: (statistics.distribution || []).slice(0, 30),
        excludedProjects: (statistics.excludedProjects || []).slice(0, 30),
      }),
    }),
    timeout: 70000,
  })

  if (response?.success === false || (response?.code !== undefined && ![0, 200, "0", "200"].includes(response.code))) {
    throw new Error(response?.message || response?.msg || "DeepSeek分析建议生成失败")
  }
  return normalizeResponseData(response)
}
