import { grasslandYakRows } from "@/views/gdMap/livestockData"

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

const toWanMu = (value) => round((Number(value) || 0) / 10000, 2)

export const pastureHouseholdTotal = 9110

const scaleRowsToTotal = (rows, total, valueKey = "number") => {
  const sourceTotal = rows.reduce((sum, item) => sum + (Number(item[valueKey]) || 0), 0)
  if (!sourceTotal || !total) return rows.map((item) => ({ ...item, [valueKey]: 0 }))
  const scaledRows = rows.map((item) => ({
    ...item,
    [valueKey]: Math.round(((Number(item[valueKey]) || 0) / sourceTotal) * total),
  }))
  const delta = total - scaledRows.reduce((sum, item) => sum + (Number(item[valueKey]) || 0), 0)
  if (delta && scaledRows.length) {
    const targetIndex = scaledRows.reduce((bestIndex, item, index) => (
      Number(item[valueKey]) > Number(scaledRows[bestIndex]?.[valueKey] || 0) ? index : bestIndex
    ), 0)
    scaledRows[targetIndex][valueKey] += delta
  }
  return scaledRows
}

const pastureTownSourceRows = scaleRowsToTotal([
  { areaCode: "513233100", areaName: "邛溪镇", number: 1452, year: 2024 },
  { areaCode: "513233101", areaName: "刷经寺镇", number: 1500, year: 2024 },
  { areaCode: "513233103", areaName: "安曲镇", number: 76, year: 2024 },
  { areaCode: "513233105", areaName: "龙日镇", number: 828, year: 2024 },
  { areaCode: "513233202", areaName: "江茸乡", number: 902, year: 2024 },
  { areaCode: "513233203", areaName: "查尔玛乡", number: 450, year: 2024 },
  { areaCode: "513233204", areaName: "瓦切镇", number: 602, year: 2024 },
  { areaCode: "513233205", areaName: "阿木乡", number: 300, year: 2024 },
  { areaCode: "513233207", areaName: "麦洼乡", number: 1204, year: 2024 },
  { areaCode: "513233208", areaName: "色地镇", number: 2638, year: 2024 },
], pastureHouseholdTotal)

const cxptCoreRows = [
  { key: "pasture-total", name: "牧户数量", value: pastureHouseholdTotal, unit: "户", icon: "town" },
  { key: "avg-yak-per-household", name: "户均牦牛头数", value: 52, unit: "头", icon: "stack" },
  { key: "avg-grassland-area", name: "户均草地面积", value: 1131, unit: "亩", icon: "area" },
  { key: "joint-household-count", name: "联户数量", value: 237, unit: "户", icon: "town" },
  { key: "poverty-monitor-count", name: "防反贫监测户", value: 122, unit: "户", icon: "town" },
  { key: "average-income", name: "人均收入", value: 19679, unit: "元/年", icon: "rate" },
]

export const pastureCoreRows = cxptCoreRows

export const pastureTopStats = cxptCoreRows.map((item) => ({
  ...item,
  zh: item.name,
  decimals: 0,
}))

export const pastureTownRows = pastureTownSourceRows.map((item) => ({
  ...item,
  key: item.areaCode,
  name: item.areaName,
  value: item.number,
  unit: "户",
}))

export const pastureOnlineRows = [
  { areaCode: "513233105", areaName: "龙日镇", uplineRate: 4, year: 2024 },
  { areaCode: "513233207", areaName: "麦洼乡", uplineRate: 52, year: 2024 },
  { areaCode: "513233103", areaName: "安曲镇", uplineRate: 22, year: 2024 },
  { areaCode: "513233100", areaName: "邛溪镇", uplineRate: 23, year: 2024 },
  { areaCode: "513233208", areaName: "色地镇", uplineRate: 8, year: 2024 },
  { areaCode: "513233205", areaName: "阿木乡", uplineRate: 12, year: 2024 },
  { areaCode: "513233202", areaName: "江茸乡", uplineRate: 14, year: 2024 },
  { areaCode: "513233203", areaName: "查尔玛乡", uplineRate: 18, year: 2024 },
  { areaCode: "513233204", areaName: "瓦切镇", uplineRate: 16, year: 2024 },
  { areaCode: "513233101", areaName: "刷经寺镇", uplineRate: 9, year: 2024 },
].map((item) => ({
  ...item,
  key: item.areaCode,
  name: item.areaName,
  value: item.uplineRate,
  unit: "%",
  extra: `${item.uplineRate}%`,
}))

export const pastureStructureRows = [
  { areaName: "全县", ageRange: "0-20", sex: "男", number: 311, value: 311, year: 2024 },
  { areaName: "全县", ageRange: "0-20", sex: "女", number: 319, value: 319, year: 2024 },
  { areaName: "全县", ageRange: "21-40", sex: "男", number: 1556, value: 1556, year: 2024 },
  { areaName: "全县", ageRange: "21-40", sex: "女", number: 1868, value: 1868, year: 2024 },
  { areaName: "全县", ageRange: "41-60", sex: "男", number: 1868, value: 1868, year: 2024 },
  { areaName: "全县", ageRange: "41-60", sex: "女", number: 1925, value: 1925, year: 2024 },
  { areaName: "全县", ageRange: "61-80", sex: "男", number: 622, value: 622, year: 2024 },
  { areaName: "全县", ageRange: "61-80", sex: "女", number: 531, value: 531, year: 2024 },
  { areaName: "全县", ageRange: "81-100", sex: "男", number: 55, value: 55, year: 2024 },
  { areaName: "全县", ageRange: "81-100", sex: "女", number: 55, value: 55, year: 2024 },
]

const aggregateBy = (rows, nameKey, valueKey, unit) => Array.from(rows.reduce((map, item) => {
  const name = item[nameKey] || "未标注"
  const row = map.get(name) || { key: name, name, value: 0, unit }
  row.value += Number(item[valueKey]) || 0
  map.set(name, row)
  return map
}, new Map()).values())

export const pastureAgeRows = aggregateBy(pastureStructureRows, "ageRange", "value", "人")
export const pastureGenderRows = aggregateBy(pastureStructureRows, "sex", "value", "人")

export const pastureListRows = [
  { name: "更洼尔", areaName: "瓦切镇", acreage: 4704.87, summerAcreage: 2267.26, winterAcreage: 2437.61, year: 2024 },
  { name: "涉尔丹", areaName: "麦洼乡", acreage: 1426.53, summerAcreage: 687.82, winterAcreage: 738.71, year: 2024 },
  { name: "热布丹", areaName: "查尔玛乡", acreage: 2176.09, summerAcreage: 1049.37, winterAcreage: 1126.72, year: 2024 },
  { name: "王西卓玛", areaName: "阿木乡", acreage: 4126.13, summerAcreage: 1989.59, winterAcreage: 2136.54, year: 2024 },
  { name: "阿尔", areaName: "色地镇", acreage: 1249.48, summerAcreage: 602.25, winterAcreage: 647.23, year: 2024 },
  { name: "尕让吉", areaName: "邛溪镇", acreage: 3688.72, summerAcreage: 1778.56, winterAcreage: 1910.16, year: 2024 },
  { name: "华尔甲", areaName: "安曲镇", acreage: 2956.44, summerAcreage: 1425.01, winterAcreage: 1531.43, year: 2024 },
  { name: "泽让措", areaName: "江茸乡", acreage: 3448.36, summerAcreage: 1662.61, winterAcreage: 1785.75, year: 2024 },
  { name: "扎西让布", areaName: "龙日镇", acreage: 2689.1, summerAcreage: 1296.15, winterAcreage: 1392.95, year: 2024 },
  { name: "格桑卓玛", areaName: "刷经寺镇", acreage: 3187.64, summerAcreage: 1536.84, winterAcreage: 1650.8, year: 2024 },
].map((item) => ({
  ...item,
  township: item.areaName,
  grasslandArea: item.acreage,
  value: item.acreage,
  unit: "亩",
  canLocate: false,
  locateText: "--",
  searchText: `${item.name} ${item.areaName}`,
}))

export const pastureGrasslandRows = grasslandYakRows.map((item) => ({
  key: item.zoneName,
  name: item.zoneName,
  value: toWanMu(item.grasslandArea),
  unit: "万亩",
  extra: "草场资源",
}))

export const pastureSeasonRows = Array.from(pastureListRows.reduce((map, item) => {
  const name = item.township || "未填写"
  const row = map.get(name) || { key: `${name}-season`, name, values: { summer: 0, winter: 0 } }
  row.values.summer += Number(item.summerAcreage) || 0
  row.values.winter += Number(item.winterAcreage) || 0
  map.set(name, row)
  return map
}, new Map()).values()).map((item) => ({
  ...item,
  values: {
    summer: toWanMu(item.values.summer),
    winter: toWanMu(item.values.winter),
  },
  unit: "万亩",
}))

export const pastureMapRows = pastureTownRows.map((item) => ({
  name: item.name,
  value: item.value,
  unit: "户",
}))

export const pastureFallbackProfile = {
  topStats: pastureTopStats,
  coreRows: pastureCoreRows,
  townRows: pastureTownRows,
  ageRows: pastureStructureRows,
  genderRows: pastureGenderRows,
  onlineRows: pastureOnlineRows,
  list: pastureListRows,
}
