import type { UnitCategory, UnitDefinition, UnitDimension } from "./types";

function unit(
  partial: Omit<UnitDefinition, "isActive"> & { isActive?: boolean }
): UnitDefinition {
  return { isActive: true, offsetToBase: 0, ...partial };
}

/** 内置农业单位表：维度 + 基准单位 + factor_to_base */
export const UNIT_REGISTRY: UnitDefinition[] = [
  // ── 质量 → kg ──
  unit({
    id: "UNIT_MG",
    unitCode: "MG",
    ucumCode: "mg",
    nameZh: "毫克",
    symbol: "mg",
    unitCategory: "mass",
    dimension: "mass",
    baseUnitCode: "KG",
    factorToBase: 0.000001,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_G",
    unitCode: "G",
    ucumCode: "g",
    nameZh: "克",
    symbol: "g",
    unitCategory: "mass",
    dimension: "mass",
    baseUnitCode: "KG",
    factorToBase: 0.001,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_KG",
    unitCode: "KG",
    ucumCode: "kg",
    nameZh: "千克",
    symbol: "kg",
    unitCategory: "mass",
    dimension: "mass",
    baseUnitCode: "KG",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_T",
    unitCode: "T",
    ucumCode: "t",
    nameZh: "吨",
    symbol: "t",
    unitCategory: "mass",
    dimension: "mass",
    baseUnitCode: "KG",
    factorToBase: 1000,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 面积 → m² ──
  unit({
    id: "UNIT_M2",
    unitCode: "M2",
    ucumCode: "m2",
    nameZh: "平方米",
    symbol: "m²",
    unitCategory: "area",
    dimension: "area",
    baseUnitCode: "M2",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_HA",
    unitCode: "HA",
    ucumCode: "ha",
    nameZh: "公顷",
    symbol: "ha",
    unitCategory: "area",
    dimension: "area",
    baseUnitCode: "M2",
    factorToBase: 10000,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_KM2",
    unitCode: "KM2",
    ucumCode: "km2",
    nameZh: "平方千米",
    symbol: "km²",
    unitCategory: "area",
    dimension: "area",
    baseUnitCode: "M2",
    factorToBase: 1_000_000,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_MU",
    unitCode: "MU",
    nameZh: "亩",
    symbol: "亩",
    unitCategory: "area",
    dimension: "area",
    baseUnitCode: "M2",
    factorToBase: 666.6666666667,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 长度 → m ──
  unit({
    id: "UNIT_MM",
    unitCode: "MM",
    ucumCode: "mm",
    nameZh: "毫米",
    symbol: "mm",
    unitCategory: "length",
    dimension: "length",
    baseUnitCode: "M",
    factorToBase: 0.001,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_CM",
    unitCode: "CM",
    ucumCode: "cm",
    nameZh: "厘米",
    symbol: "cm",
    unitCategory: "length",
    dimension: "length",
    baseUnitCode: "M",
    factorToBase: 0.01,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_M",
    unitCode: "M",
    ucumCode: "m",
    nameZh: "米",
    symbol: "m",
    unitCategory: "length",
    dimension: "length",
    baseUnitCode: "M",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 体积 → L ──
  unit({
    id: "UNIT_ML",
    unitCode: "ML",
    ucumCode: "mL",
    nameZh: "毫升",
    symbol: "mL",
    unitCategory: "volume",
    dimension: "volume",
    baseUnitCode: "L",
    factorToBase: 0.001,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_L",
    unitCode: "L",
    ucumCode: "L",
    nameZh: "升",
    symbol: "L",
    unitCategory: "volume",
    dimension: "volume",
    baseUnitCode: "L",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_M3",
    unitCode: "M3",
    ucumCode: "m3",
    nameZh: "立方米",
    symbol: "m³",
    unitCategory: "volume",
    dimension: "volume",
    baseUnitCode: "L",
    factorToBase: 1000,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 计数 / 包装 ──
  unit({
    id: "UNIT_SEED",
    unitCode: "SEED",
    ucumCode: "1",
    nameZh: "粒",
    symbol: "粒",
    unitCategory: "count",
    dimension: "count",
    baseUnitCode: "SEED",
    factorToBase: 1,
    isConvertible: false,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_BAG",
    unitCode: "BAG",
    nameZh: "袋",
    symbol: "袋",
    unitCategory: "package",
    dimension: "package",
    isConvertible: false,
    isRequiredSpec: true,
  }),
  unit({
    id: "UNIT_TUBE",
    unitCode: "TUBE",
    nameZh: "管",
    symbol: "管",
    unitCategory: "package",
    dimension: "package",
    isConvertible: false,
    isRequiredSpec: true,
  }),
  unit({
    id: "UNIT_BOX",
    unitCode: "BOX",
    nameZh: "盒",
    symbol: "盒",
    unitCategory: "package",
    dimension: "package",
    isConvertible: false,
    isRequiredSpec: true,
  }),
  unit({
    id: "UNIT_PACK",
    unitCode: "PACK",
    nameZh: "包",
    symbol: "包",
    unitCategory: "package",
    dimension: "package",
    isConvertible: false,
    isRequiredSpec: true,
  }),
  unit({
    id: "UNIT_BOTTLE",
    unitCode: "BOTTLE",
    nameZh: "瓶",
    symbol: "瓶",
    unitCategory: "package",
    dimension: "package",
    isConvertible: false,
    isRequiredSpec: true,
  }),

  // ── 产量 → kg/ha ──
  unit({
    id: "UNIT_KG_HA",
    unitCode: "KG_HA",
    ucumCode: "kg/ha",
    nameZh: "千克每公顷",
    symbol: "kg/ha",
    unitCategory: "yield",
    dimension: "yield",
    baseUnitCode: "KG_HA",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_T_HA",
    unitCode: "T_HA",
    ucumCode: "t/ha",
    nameZh: "吨每公顷",
    symbol: "t/ha",
    unitCategory: "yield",
    dimension: "yield",
    baseUnitCode: "KG_HA",
    factorToBase: 1000,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_KG_MU",
    unitCode: "KG_MU",
    nameZh: "千克每亩",
    symbol: "kg/亩",
    unitCategory: "yield",
    dimension: "yield",
    baseUnitCode: "KG_HA",
    factorToBase: 15,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_JIN_MU",
    unitCode: "JIN_MU",
    nameZh: "斤每亩",
    symbol: "斤/亩",
    unitCategory: "yield",
    dimension: "yield",
    baseUnitCode: "KG_HA",
    factorToBase: 7.5,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 施肥/用药（质量率）→ kg/ha ──
  unit({
    id: "UNIT_APP_KG_HA",
    unitCode: "APP_KG_HA",
    ucumCode: "kg/ha",
    nameZh: "千克每公顷",
    symbol: "kg/ha",
    unitCategory: "application_rate",
    dimension: "application_rate_mass",
    baseUnitCode: "APP_KG_HA",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_APP_G_HA",
    unitCode: "APP_G_HA",
    ucumCode: "g/ha",
    nameZh: "克每公顷",
    symbol: "g/ha",
    unitCategory: "application_rate",
    dimension: "application_rate_mass",
    baseUnitCode: "APP_KG_HA",
    factorToBase: 0.001,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_APP_KG_MU",
    unitCode: "APP_KG_MU",
    nameZh: "千克每亩",
    symbol: "kg/亩",
    unitCategory: "application_rate",
    dimension: "application_rate_mass",
    baseUnitCode: "APP_KG_HA",
    factorToBase: 15,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_APP_G_MU",
    unitCode: "APP_G_MU",
    nameZh: "克每亩",
    symbol: "g/亩",
    unitCategory: "application_rate",
    dimension: "application_rate_mass",
    baseUnitCode: "APP_KG_HA",
    factorToBase: 0.015,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 用药（体积率）→ L/ha ──
  unit({
    id: "UNIT_APP_L_HA",
    unitCode: "APP_L_HA",
    ucumCode: "L/ha",
    nameZh: "升每公顷",
    symbol: "L/ha",
    unitCategory: "application_rate",
    dimension: "application_rate_volume",
    baseUnitCode: "APP_L_HA",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_APP_ML_MU",
    unitCode: "APP_ML_MU",
    nameZh: "毫升每亩",
    symbol: "mL/亩",
    unitCategory: "application_rate",
    dimension: "application_rate_volume",
    baseUnitCode: "APP_L_HA",
    factorToBase: 0.015,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 种植密度 → plants/ha ──
  unit({
    id: "UNIT_PLANTS_HA",
    unitCode: "PLANTS_HA",
    nameZh: "株每公顷",
    symbol: "株/ha",
    unitCategory: "plant_density",
    dimension: "plant_density",
    baseUnitCode: "PLANTS_HA",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_PLANTS_MU",
    unitCode: "PLANTS_MU",
    nameZh: "株每亩",
    symbol: "株/亩",
    unitCategory: "plant_density",
    dimension: "plant_density",
    baseUnitCode: "PLANTS_HA",
    factorToBase: 15,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_PLANTS_M2",
    unitCode: "PLANTS_M2",
    nameZh: "株每平方米",
    symbol: "株/m²",
    unitCategory: "plant_density",
    dimension: "plant_density",
    baseUnitCode: "PLANTS_HA",
    factorToBase: 10000,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 降雨/灌溉 → mm ──
  unit({
    id: "UNIT_WATER_MM",
    unitCode: "WATER_MM",
    ucumCode: "mm",
    nameZh: "毫米水深",
    symbol: "mm",
    unitCategory: "water",
    dimension: "water_depth",
    baseUnitCode: "WATER_MM",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_WATER_M3_HA",
    unitCode: "WATER_M3_HA",
    nameZh: "立方米每公顷",
    symbol: "m³/ha",
    unitCategory: "water",
    dimension: "water_depth",
    baseUnitCode: "WATER_MM",
    factorToBase: 0.1,
    isConvertible: true,
    isRequiredSpec: false,
  }),

  // ── 百分比 / 表型 Scale ──
  unit({
    id: "UNIT_PERCENT",
    unitCode: "PERCENT",
    ucumCode: "%",
    nameZh: "百分比",
    symbol: "%",
    unitCategory: "percent",
    dimension: "percent",
    baseUnitCode: "PERCENT",
    factorToBase: 1,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_PPM",
    unitCode: "PPM",
    ucumCode: "ppm",
    nameZh: "百万分之一",
    symbol: "ppm",
    unitCategory: "percent",
    dimension: "percent",
    baseUnitCode: "PERCENT",
    factorToBase: 0.0001,
    isConvertible: true,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_DAY",
    unitCode: "DAY",
    ucumCode: "d",
    nameZh: "天",
    symbol: "d",
    unitCategory: "scale",
    dimension: "scale",
    baseUnitCode: "DAY",
    factorToBase: 1,
    isConvertible: false,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_PIECE",
    unitCode: "PIECE",
    nameZh: "个",
    symbol: "个",
    unitCategory: "scale",
    dimension: "scale",
    baseUnitCode: "PIECE",
    factorToBase: 1,
    isConvertible: false,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_SCORE",
    unitCode: "SCORE",
    nameZh: "分",
    symbol: "分",
    unitCategory: "scale",
    dimension: "scale",
    baseUnitCode: "SCORE",
    factorToBase: 1,
    isConvertible: false,
    isRequiredSpec: false,
  }),
  unit({
    id: "UNIT_GRADE_1_9",
    unitCode: "GRADE_1_9",
    nameZh: "1-9等级",
    symbol: "1-9",
    unitCategory: "scale",
    dimension: "scale",
    baseUnitCode: "GRADE_1_9",
    factorToBase: 1,
    isConvertible: false,
    isRequiredSpec: false,
  }),
];

const byCode = new Map<string, UnitDefinition>();
const byDimension = new Map<UnitDimension, UnitDefinition[]>();

for (const item of UNIT_REGISTRY) {
  byCode.set(item.unitCode, item);
  const list = byDimension.get(item.dimension) ?? [];
  list.push(item);
  byDimension.set(item.dimension, list);
}

export function getUnitByCode(code: string): UnitDefinition | undefined {
  return byCode.get(code);
}

export function getUnitsByDimension(dimension: UnitDimension): UnitDefinition[] {
  return (byDimension.get(dimension) ?? []).filter((u) => u.isActive !== false);
}

export function getUnitsByCategory(category: UnitCategory): UnitDefinition[] {
  return UNIT_REGISTRY.filter((u) => u.unitCategory === category && u.isActive !== false);
}

export function getConvertibleUnits(dimension: UnitDimension): UnitDefinition[] {
  return getUnitsByDimension(dimension).filter((u) => u.isConvertible);
}

export const DIMENSION_LABELS: Record<UnitDimension, string> = {
  mass: "质量",
  area: "面积",
  length: "长度",
  volume: "体积",
  count: "计数",
  package: "包装",
  yield: "产量",
  application_rate_mass: "施肥/用药（质量）",
  application_rate_volume: "施肥/用药（体积）",
  plant_density: "种植密度",
  water_depth: "降雨/灌溉",
  percent: "百分比",
  scale: "表型等级",
};

export const CATEGORY_LABELS: Record<UnitCategory, string> = {
  mass: "质量",
  area: "面积",
  length: "长度",
  volume: "体积",
  count: "计数",
  package: "包装",
  yield: "产量",
  application_rate: "农事投入",
  plant_density: "种植密度",
  water: "水分/灌溉",
  percent: "百分比",
  scale: "表型 Scale",
};

/** 演示页场景分组 */
export const UNIT_DEMO_GROUPS = [
  {
    id: "seedlot",
    title: "SeedLot 库存",
    description: "粒、质量单位与包装单位；包装需规格",
    dimension: "mass" as UnitDimension,
    extraDimensions: ["count" as UnitDimension, "package" as UnitDimension],
    defaultFrom: "KG",
    defaultTo: "G",
    examples: [
      { value: 2.5, from: "KG", to: "G", label: "2.5 kg → g" },
      { value: 1, from: "T", to: "KG", label: "1 t → kg" },
    ],
  },
  {
    id: "phenotype",
    title: "表型 Scale",
    description: "长度、质量、百分比、等级等观测单位",
    dimension: "length" as UnitDimension,
    extraDimensions: ["mass" as UnitDimension, "percent" as UnitDimension, "scale" as UnitDimension],
    defaultFrom: "CM",
    defaultTo: "M",
    examples: [{ value: 168, from: "CM", to: "M", label: "株高 168 cm → m" }],
  },
  {
    id: "field-area",
    title: "田间面积",
    description: "m²、ha、亩统一换算",
    dimension: "area" as UnitDimension,
    defaultFrom: "MU",
    defaultTo: "HA",
    examples: [{ value: 15, from: "MU", to: "HA", label: "15 亩 → ha" }],
  },
  {
    id: "yield",
    title: "产量",
    description: "统一内部标准 kg/ha",
    dimension: "yield" as UnitDimension,
    defaultFrom: "KG_MU",
    defaultTo: "KG_HA",
    examples: [
      { value: 650, from: "KG_MU", to: "KG_HA", label: "650 kg/亩 → kg/ha" },
      { value: 9.75, from: "T_HA", to: "KG_HA", label: "9.75 t/ha → kg/ha" },
    ],
  },
  {
    id: "application",
    title: "农事投入",
    description: "施肥/用药质量率与体积率",
    dimension: "application_rate_mass" as UnitDimension,
    extraDimensions: ["application_rate_volume" as UnitDimension],
    defaultFrom: "APP_KG_MU",
    defaultTo: "APP_KG_HA",
    examples: [
      { value: 30, from: "APP_KG_MU", to: "APP_KG_HA", label: "30 kg/亩 → kg/ha" },
      { value: 100, from: "APP_ML_MU", to: "APP_L_HA", label: "100 mL/亩 → L/ha" },
    ],
  },
  {
    id: "density",
    title: "种植密度",
    description: "株/ha、株/亩、株/m²",
    dimension: "plant_density" as UnitDimension,
    defaultFrom: "PLANTS_MU",
    defaultTo: "PLANTS_HA",
    examples: [{ value: 4000, from: "PLANTS_MU", to: "PLANTS_HA", label: "4000 株/亩 → 株/ha" }],
  },
  {
    id: "water",
    title: "水分/灌溉",
    description: "mm 与 m³/ha 互转",
    dimension: "water_depth" as UnitDimension,
    defaultFrom: "WATER_MM",
    defaultTo: "WATER_M3_HA",
    examples: [{ value: 25, from: "WATER_MM", to: "WATER_M3_HA", label: "25 mm → m³/ha" }],
  },
] as const;
