import { estimatedStock, grasslandWetRows, grasslandYakRows, homeCore, supportIndustryRows, tradeImpactRows } from "@/views/gdMap/livestockData"

const round = (value, decimals = 2) => {
  const factor = 10 ** decimals
  return Math.round((Number(value) || 0) * factor) / factor
}

const sumBy = (rows, key) => rows.reduce((sum, item) => sum + (Number(item[key]) || 0), 0)
const toWanMu = (value) => round((Number(value) || 0) / 10000, 2)

const totalTradeHead = sumBy(tradeImpactRows, "value")
const totalTownYak = sumBy(grasslandYakRows, "yakCount")
const yakStockTotal = estimatedStock
const grasslandAreaWanMu = round(homeCore.grasslandArea / 10000, 2)
const wetlandAreaWanMu = round(homeCore.wetArea / 10000, 2)
const woodlandAreaWanMu = 177.92
const totalForestGrassWetWanMu = round(grasslandAreaWanMu + wetlandAreaWanMu + woodlandAreaWanMu, 2)
const yakDensity = round(yakStockTotal / Math.max(grasslandAreaWanMu, 1), 1)
const wetlandRate = round((wetlandAreaWanMu / Math.max(totalForestGrassWetWanMu, 1)) * 100, 1)
const basicGrasslandWanMu = 637.07
const wetlandYieldWanTon = 108.84
const grasslandYieldWanTon = 297.29
const woodlandPublicWanMu = 159.33

export const homeTopStats = [
  {
    key: "yak-count",
    zh: "牦牛数量",
    name: "牦牛数量",
    value: yakStockTotal,
    unit: "头",
    decimals: 0,
  },
  {
    key: "grassland-area",
    zh: "草地面积",
    name: "草地面积",
    value: grasslandAreaWanMu,
    unit: "万亩",
    decimals: 2,
  },
  {
    key: "wetland-area",
    zh: "湿地面积",
    name: "湿地面积",
    value: wetlandAreaWanMu,
    unit: "万亩",
    decimals: 2,
  },
  {
    key: "woodland-area",
    zh: "林地面积",
    name: "林地面积",
    value: woodlandAreaWanMu,
    unit: "万亩",
    decimals: 2,
  },
]

export const homeHeroStats = [
  { key: "grassland", name: "草地面积", value: grasslandAreaWanMu, unit: "万亩", tone: "green" },
  { key: "wetland", name: "湿地面积", value: wetlandAreaWanMu, unit: "万亩", tone: "cyan" },
  { key: "woodland", name: "林地面积", value: woodlandAreaWanMu, unit: "万亩", tone: "mint" },
  { key: "yak-density", name: "草畜密度", value: yakDensity, unit: "头/万亩", tone: "gold" },
]

export const resourceCompositionRows = [
  { key: "grassland", name: "草地", value: grasslandAreaWanMu, unit: "万亩", color: "#7ee269" },
  { key: "wetland", name: "湿地", value: wetlandAreaWanMu, unit: "万亩", color: "#32dcff" },
  { key: "woodland", name: "林地", value: woodlandAreaWanMu, unit: "万亩", color: "#24f6c4" },
]

export const businessMatrixRows = [
  {
    key: "basic-grassland",
    name: "基本草原",
    value: basicGrasslandWanMu,
    unit: "万亩",
    percent: round((basicGrasslandWanMu / Math.max(grasslandAreaWanMu, 1)) * 100, 1),
    note: "草地保护底盘",
  },
  {
    key: "wetland-yield",
    name: "湿地生产力",
    value: wetlandYieldWanTon,
    unit: "万吨",
    percent: round((wetlandYieldWanTon / Math.max(grasslandYieldWanTon + wetlandYieldWanTon, 1)) * 100, 1),
    note: "鲜草产能",
  },
  {
    key: "public-woodland",
    name: "公益林面积",
    value: woodlandPublicWanMu,
    unit: "万亩",
    percent: round((woodlandPublicWanMu / Math.max(woodlandAreaWanMu, 1)) * 100, 1),
    note: "林地保护占比",
  },
  {
    key: "wetland-rate",
    name: "湿地占比",
    value: wetlandRate,
    unit: "%",
    percent: wetlandRate,
    note: "林草湿结构",
  },
]

export const townSynergyRows = grasslandWetRows.map((item) => {
  const yakRow = grasslandYakRows.find((row) => row.zoneName === item.zoneName) || {}
  const grasslandArea = toWanMu(item.grasslandArea)
  const wetlandArea = toWanMu(item.wetArea)
  return {
    key: item.zoneName,
    name: item.zoneName,
    grasslandArea,
    wetlandArea,
    yakCount: yakRow.yakCount || 0,
    resourceTotal: round(grasslandArea + wetlandArea, 2),
    density: round((yakRow.yakCount || 0) / Math.max(grasslandArea + wetlandArea, 1), 1),
  }
}).sort((a, b) => b.yakCount - a.yakCount)

export const yakTownRows = grasslandYakRows
  .map((item) => ({
    key: item.zoneName,
    name: item.zoneName,
    value: item.yakCount,
    unit: "头",
    percent: round((item.yakCount / Math.max(totalTownYak, 1)) * 100, 1),
  }))
  .sort((a, b) => b.value - a.value)

export const tradeRows = tradeImpactRows.map((item) => ({
  key: item.label,
  name: item.label,
  value: item.value,
  unit: "头",
  yoy: item.yoy,
  percent: round((item.value / Math.max(totalTradeHead, 1)) * 100, 1),
}))

const maxSupportValue = Math.max(...supportIndustryRows.map((item) => Number(item.value) || 0), 1)

export const supportRows = supportIndustryRows.map((item) => {
  const capacityNote = item.name.includes("储备") || item.name.includes("加工")
    ? `${Number(item.capacity || 0).toLocaleString("zh-CN")}吨`
    : "已接入"
  return {
    key: item.name,
    name: item.name,
    value: item.value,
    unit: item.unit,
    capacity: item.capacity,
    capacityNote,
    percent: round(((Number(item.value) || 0) / maxSupportValue) * 100, 1),
  }
})

export const dataFlowRows = [
  { key: "flow-grassland", module: "林草湿", name: "草地面积分布完成乡镇归集", value: grasslandAreaWanMu, unit: "万亩", trend: "+0.8%" },
  { key: "flow-wetland", module: "林草湿", name: "湿地生产力指标进入总览", value: wetlandYieldWanTon, unit: "万吨", trend: "+1.2%" },
  { key: "flow-woodland", module: "林草湿", name: "公益林保护面积同步", value: woodlandPublicWanMu, unit: "万亩", trend: "89.6%" },
  { key: "flow-yak-town", module: "牦牛", name: "畜群结构总量同步", value: yakStockTotal, unit: "头", trend: "+2.4%" },
  { key: "flow-yak-density", module: "草畜", name: "草畜密度联动核算", value: yakDensity, unit: "头/万亩", trend: "联动" },
  { key: "flow-trade-1", module: "交易", name: "屠宰出栏流通记录入库", value: tradeRows[0]?.value || 0, unit: "头", trend: "+6%" },
  { key: "flow-trade-2", module: "交易", name: "活畜交易影响扣减存栏", value: tradeRows[1]?.value || 0, unit: "头", trend: "-3%" },
  { key: "flow-industry", module: "产业", name: "饲草储备与加工主体纳入画像", value: homeCore.baseCount + homeCore.projectCount, unit: "个", trend: "在线" },
]

export const centerOrbitNodes = [
  { key: "grassland", label: "草地", value: grasslandAreaWanMu, unit: "万亩", x: 50, y: 15, color: "#7ee269" },
  { key: "wetland", label: "湿地", value: wetlandAreaWanMu, unit: "万亩", x: 83, y: 48, color: "#32dcff" },
  { key: "trade", label: "交易", value: totalTradeHead, unit: "头", x: 68, y: 72, color: "#ffe06b" },
  { key: "yak", label: "牦牛", value: yakStockTotal, unit: "头", x: 24, y: 74, color: "#24f6c4" },
  { key: "woodland", label: "林地", value: woodlandAreaWanMu, unit: "万亩", x: 16, y: 35, color: "#2bd17e" },
]
