// Data mapped from cxph-jgpt real system screens and static data files.
export const YAK_STOCK_TOTAL = 458230;
export const YAK_DRONE_RECOGNITION_TOTAL = 450817;
export const YAK_EXPERT_CORRECTION = YAK_STOCK_TOTAL - YAK_DRONE_RECOGNITION_TOTAL;

function scaleCountRowsToTotal(rows, total, key = "count") {
  const sourceTotal = rows.reduce((sum, item) => sum + (Number(item[key]) || 0), 0);
  if (!sourceTotal || !total) return rows.map((item) => ({ ...item, [key]: 0 }));
  const scaledRows = rows.map((item) => ({
    ...item,
    [key]: Math.round(((Number(item[key]) || 0) / sourceTotal) * total),
  }));
  const delta = total - scaledRows.reduce((sum, item) => sum + (Number(item[key]) || 0), 0);
  if (delta && scaledRows[0]) scaledRows[0][key] += delta;
  return scaledRows;
}

export const homeCore = {
  herdsmanCount: 129,
  grasslandArea: 7563700,
  wetArea: 2918200,
  yakCount: YAK_STOCK_TOTAL,
  projectCount: 17,
  baseCount: 12,
};

const rawGrasslandYakRows = [
  { zoneName: "色地镇", grasslandArea: 1463044, yakCount: 11704 },
  { zoneName: "麦洼乡", grasslandArea: 660352, yakCount: 5283 },
  { zoneName: "阿木乡", grasslandArea: 1115947, yakCount: 8928 },
  { zoneName: "瓦切镇", grasslandArea: 1111492, yakCount: 8892 },
  { zoneName: "查尔玛乡", grasslandArea: 858500, yakCount: 6868 },
  { zoneName: "江茸乡", grasslandArea: 644761, yakCount: 5158 },
  { zoneName: "龙日镇", grasslandArea: 874234, yakCount: 6994 },
  { zoneName: "安曲镇", grasslandArea: 1165634, yakCount: 9325 },
  { zoneName: "刷经寺镇", grasslandArea: 62465, yakCount: 500 },
  { zoneName: "邛溪镇", grasslandArea: 1166721, yakCount: 9334 },
];

export const grasslandWetRows = [
  { zoneName: "色地镇", grasslandArea: 350788, wetArea: 166983 },
  { zoneName: "麦洼乡", grasslandArea: 162198, wetArea: 71501 },
  { zoneName: "阿木乡", grasslandArea: 305522, wetArea: 89411 },
  { zoneName: "瓦切镇", grasslandArea: 264640, wetArea: 128717 },
  { zoneName: "查尔玛乡", grasslandArea: 286159, wetArea: 17664 },
  { zoneName: "江茸乡", grasslandArea: 175840, wetArea: 52341 },
  { zoneName: "龙日镇", grasslandArea: 222346, wetArea: 87046 },
  { zoneName: "安曲镇", grasslandArea: 304970, wetArea: 107548 },
  { zoneName: "刷经寺镇", grasslandArea: 22107, wetArea: 7002 },
  { zoneName: "邛溪镇", grasslandArea: 318316, wetArea: 94587 },
];

export const yakForecastRows = [
  { month: "1月", count: 458230, speed: 0 },
  { month: "2月", count: 458860, speed: 630 },
  { month: "3月", count: 459520, speed: 660 },
  { month: "4月", count: 460180, speed: 660 },
  { month: "5月", count: 461260, speed: 1080 },
  { month: "6月", count: 462050, speed: 790 },
  { month: "7月", count: 462850, speed: 800 },
  { month: "8月", count: 463160, speed: 310 },
  { month: "9月", count: 463520, speed: 360 },
  { month: "10月", count: 463940, speed: 420 },
  { month: "11月", count: 464350, speed: 410 },
  { month: "12月", count: 464760, speed: 410 },
];

const rawTownRecognitionRows = [
  { name: "色地镇", count: 131983, devices: 25 },
  { name: "瓦切镇", count: 126611, devices: 24 },
  { name: "安曲镇", count: 99331, devices: 20 },
  { name: "邛溪镇", count: 81727, devices: 18 },
  { name: "麦洼乡", count: 78489, devices: 16 },
  { name: "龙日镇", count: 66846, devices: 15 },
  { name: "阿木乡", count: 64176, devices: 14 },
  { name: "查尔玛乡", count: 47958, devices: 11 },
  { name: "江茸乡", count: 34576, devices: 9 },
  { name: "刷经寺镇", count: 30407, devices: 8 },
];

export const townRecognitionRows = scaleCountRowsToTotal(rawTownRecognitionRows, YAK_DRONE_RECOGNITION_TOTAL);
const finalTownCountMap = new Map(scaleCountRowsToTotal(rawTownRecognitionRows, YAK_STOCK_TOTAL).map((item) => [item.name, item.count]));
export const grasslandYakRows = rawGrasslandYakRows.map((item) => ({
  ...item,
  yakCount: finalTownCountMap.get(item.zoneName) || item.yakCount,
}));
export const recognitionTotal = townRecognitionRows.reduce((sum, item) => sum + item.count, 0);
export const expertCorrection = YAK_EXPERT_CORRECTION;
export const tradeDeduction = 0;
export const estimatedStock = YAK_STOCK_TOTAL;
homeCore.yakCount = estimatedStock;

function withStockCounts(rows, total) {
  const nextRows = rows.map((item) => ({
    ...item,
    count: Math.round((total * item.ratio) / 100),
  }));
  const delta = total - nextRows.reduce((sum, item) => sum + item.count, 0);
  if (delta && nextRows[1]) {
    nextRows[1].count += delta;
  }
  return nextRows;
}

export const herdStructureRows = withStockCounts(
  [
    { name: "成年母牛", ratio: 47 },
    { name: "其他成年牛", ratio: 22 },
    { name: "幼畜", ratio: 16 },
    { name: "育成牛", ratio: 11 },
    { name: "公牛", ratio: 4 },
  ],
  estimatedStock,
);

export const tradeImpactRows = [
  { label: "屠宰出栏", value: 1706, yoy: 6 },
  { label: "活畜交易", value: 1043, yoy: -3 },
  { label: "外运销售", value: 624, yoy: 12 },
  { label: "其他出栏", value: 299, yoy: -9 },
];

export const stockForecastRows = [
  { name: "当前", stock: estimatedStock, births: 0, outflow: 0 },
  { name: "2026年底", stock: 462850, births: 24680, outflow: 19020 },
  { name: "2027年底", stock: 468920, births: 67420, outflow: 55180 },
  { name: "2028年底", stock: 475360, births: 109680, outflow: 90140 },
];

export const supportIndustryRows = [
  { name: "饲草储备库", value: 7, capacity: 10500, unit: "座" },
  { name: "加工企业", value: 7, capacity: 13000, unit: "家" },
  { name: "工程数量", value: homeCore.projectCount, capacity: 17, unit: "个" },
  { name: "基地数量", value: homeCore.baseCount, capacity: 12, unit: "个" },
];

export const warningRows = [
  { name: "越界预警", value: 4 },
  { name: "草畜平衡", value: 3 },
  { name: "防疫提醒", value: 2 },
  { name: "设备离线", value: 1 },
];
