import type { ComponentProps } from "react";
import {
  BookCheck,
  Briefcase,
  Calendar,
  Camera,
  ClipboardList,
  Database,
  Dna,
  FlaskConical,
  GitFork,
  Leaf,
  MapPin,
  Network,
  Package,
  Ruler,
  Sigma,
  TestTube,
  TrendingUp,
  Users,
} from "lucide-react";
import { BrapiEntityPage } from "./BrapiEntityPage";

export type BrapiEntityConfig = ComponentProps<typeof BrapiEntityPage>;

const cropOptions = [
  { value: "Maize", label: "Maize 玉米" },
  { value: "Rice", label: "Rice 水稻" },
  { value: "Wheat", label: "Wheat 小麦" },
  { value: "Soybean", label: "Soybean 大豆" },
];

export const brapiEntityConfigs: Record<string, BrapiEntityConfig> = {
  location: {
    icon: MapPin,
    iconBg: "bg-gradient-to-br from-green-500 to-teal-600",
    title: "Location 地点",
    description: "维护试验站、田块、温室、实验室等物理地点",
    addLabel: "新增地点",
    columns: [
      { key: "location_name", label: "地点名称" },
      { key: "location_type", label: "地点类型" },
      { key: "country_code", label: "国家代码" },
      { key: "crop_id", label: "归属作物" },
    ],
    fields: [
      { key: "location_name", label: "地点名称", type: "text", required: true, placeholder: "如 Beijing Research Station" },
      { key: "location_type", label: "地点类型", type: "select", options: ["试验站", "育种基地", "野外试验点", "试验农场", "温室", "实验室"].map((value) => ({ value, label: value })) },
      { key: "country_code", label: "国家代码", type: "text", placeholder: "CHN" },
      { key: "crop_id", label: "归属作物", type: "select", options: cropOptions },
      { key: "latitude", label: "纬度", type: "text", placeholder: "39.9042" },
      { key: "longitude", label: "经度", type: "text", placeholder: "116.4074" },
    ],
    data: [
      { id: "loc1", location_name: "Beijing Research Station", location_type: "试验站", country_code: "CHN", crop_id: "Maize" },
      { id: "loc2", location_name: "Hainan South Breeding Base", location_type: "育种基地", country_code: "CHN", crop_id: "Maize / Rice" },
      { id: "loc3", location_name: "Yunnan Altitude Trial Site", location_type: "野外试验点", country_code: "CHN", crop_id: "Wheat" },
    ],
  },
  season: {
    icon: Calendar,
    iconBg: "bg-gradient-to-br from-sky-500 to-blue-600",
    title: "Season 季节",
    description: "维护年度和季节，如 2026 Spring、2026 Rainy Season",
    addLabel: "新增季节",
    columns: [{ key: "year", label: "年份" }, { key: "season", label: "季节" }, { key: "season_name", label: "季节名称" }],
    fields: [
      { key: "year", label: "年份", type: "number", required: true, placeholder: "2026" },
      { key: "season", label: "季节", type: "select", required: true, options: ["Spring 春季", "Summer 夏季", "Autumn 秋季", "Winter 冬季", "Rainy 雨季", "Dry 旱季"].map((value) => ({ value, label: value })) },
      { key: "season_name", label: "季节显示名称", type: "text", placeholder: "如 2026 Spring", colSpan: 2 },
    ],
    data: [
      { id: "sea1", year: 2026, season: "Spring 春季", season_name: "2026 Spring" },
      { id: "sea2", year: 2026, season: "Rainy 雨季", season_name: "2026 Rainy" },
    ],
  },
  ontology: {
    icon: Network,
    iconBg: "bg-gradient-to-br from-amber-600 to-orange-700",
    title: "Ontology 本体",
    description: "Crop Ontology 或企业内部性状本体，是 Trait/Method/Scale 的来源标准",
    addLabel: "新增本体",
    columns: [{ key: "id", label: "本体 ID" }, { key: "ontology_name", label: "本体名称" }, { key: "version", label: "版本" }, { key: "documentationurl", label: "文档地址" }],
    fields: [
      { key: "id", label: "本体 ID", type: "text", required: true, placeholder: "如 CO_322" },
      { key: "ontology_name", label: "本体名称", type: "text", required: true, placeholder: "如 Crop Ontology - Maize" },
      { key: "version", label: "版本号", type: "text", placeholder: "3.2.0" },
      { key: "documentationurl", label: "文档地址", type: "text", colSpan: 2 },
    ],
    data: [
      { id: "CO_322", ontology_name: "Crop Ontology - Maize", version: "3.2.0", documentationurl: "https://cropontology.org/ontology/CO_322" },
      { id: "CO_319", ontology_name: "Crop Ontology - Rice", version: "5.0.0", documentationurl: "https://cropontology.org/ontology/CO_319" },
    ],
  },
  program: {
    icon: Briefcase,
    iconBg: "bg-gradient-to-br from-blue-500 to-indigo-600",
    title: "Program 项目",
    description: "育种项目、科研项目、基金项目等高层组织，是 Trial 和 Study 的入口",
    addLabel: "新增项目",
    columns: [{ key: "name", label: "项目名称" }, { key: "abbreviation", label: "缩写" }, { key: "objective", label: "目标" }, { key: "crop_id", label: "作物" }, { key: "lead_person_id", label: "负责人" }],
    fields: [
      { key: "name", label: "项目名称", type: "text", required: true },
      { key: "abbreviation", label: "缩写", type: "text" },
      { key: "crop_id", label: "作物", type: "select", options: cropOptions },
      { key: "lead_person_id", label: "负责人", type: "text" },
      { key: "objective", label: "项目目标", type: "textarea", colSpan: 2 },
    ],
    data: [
      { id: "MYIP-2026", name: "Maize Yield Improvement Program", abbreviation: "MYIP", objective: "提高玉米单产和品质", crop_id: "Maize", lead_person_id: "Li Wei" },
      { id: "RQIP-2026", name: "Rice Quality Improvement Program", abbreviation: "RQIP", objective: "改善水稻口感和营养品质", crop_id: "Rice", lead_person_id: "Zhang Ming" },
    ],
  },
  trial: {
    icon: FlaskConical,
    iconBg: "bg-gradient-to-br from-indigo-500 to-violet-600",
    title: "Trial 试验",
    description: "Program 下的试验集合，可跨地点、跨年度、跨 Study",
    addLabel: "新增试验",
    columns: [{ key: "trial_name", label: "试验名称" }, { key: "program_id", label: "项目" }, { key: "crop_id", label: "作物" }, { key: "start_date", label: "开始日期" }, { key: "active", label: "状态" }],
    fields: [
      { key: "trial_name", label: "试验名称", type: "text", required: true },
      { key: "program_id", label: "所属项目", type: "text" },
      { key: "crop_id", label: "作物", type: "select", options: cropOptions },
      { key: "start_date", label: "开始日期", type: "date" },
      { key: "end_date", label: "结束日期", type: "date" },
      { key: "trial_description", label: "试验说明", type: "textarea", colSpan: 2 },
    ],
    data: [
      { id: "tr1", trial_name: "2026 Multi Location Yield Trial", program_id: "MYIP-2026", crop_id: "Maize", start_date: "2026-03-01", active: "活跃" },
      { id: "tr2", trial_name: "2026 Rice Grain Quality Trial", program_id: "RQIP-2026", crop_id: "Rice", start_date: "2026-04-01", active: "活跃" },
    ],
  },
  study: {
    icon: BookCheck,
    iconBg: "bg-gradient-to-br from-purple-500 to-fuchsia-600",
    title: "Study 研究",
    description: "单一地点上的表型实验或 fieldbook，是数据采集的核心单元",
    addLabel: "新增 Study",
    columns: [{ key: "study_name", label: "Study 名称" }, { key: "study_code", label: "编码" }, { key: "study_type", label: "类型" }, { key: "trial_id", label: "Trial" }, { key: "location_id", label: "地点" }],
    fields: [
      { key: "study_name", label: "Study 名称", type: "text", required: true },
      { key: "study_code", label: "Study 编码", type: "text" },
      { key: "study_type", label: "Study 类型", type: "select", options: ["Phenotyping", "Genotyping", "Nursery", "Yield Trial"].map((value) => ({ value, label: value })) },
      { key: "trial_id", label: "所属 Trial", type: "text" },
      { key: "location_id", label: "地点", type: "text" },
      { key: "start_date", label: "开始日期", type: "date" },
    ],
    data: [
      { id: "st1", study_name: "2026 Beijing Yield Trial", study_code: "BJ-YLD-2026", study_type: "Phenotyping", trial_id: "2026 Multi Location Yield Trial", location_id: "Beijing Research Station" },
      { id: "st2", study_name: "2026 Hainan Yield Trial", study_code: "HN-YLD-2026", study_type: "Phenotyping", trial_id: "2026 Multi Location Yield Trial", location_id: "Hainan South Breeding Base" },
    ],
  },
  germplasm: {
    icon: Dna,
    iconBg: "bg-gradient-to-br from-teal-500 to-emerald-600",
    title: "Germplasm 种质",
    description: "品种、品系、材料、资源材料等种质核心对象",
    addLabel: "新增种质",
    columns: [{ key: "germplasm_name", label: "种质名称" }, { key: "genus", label: "属" }, { key: "species", label: "种" }, { key: "crop_id", label: "作物" }, { key: "breeding_method_id", label: "育种方法" }],
    fields: [
      { key: "germplasm_name", label: "种质名称", type: "text", required: true },
      { key: "default_display_name", label: "展示名称", type: "text" },
      { key: "genus", label: "属", type: "text" },
      { key: "species", label: "种", type: "text" },
      { key: "crop_id", label: "作物", type: "select", options: cropOptions },
      { key: "breeding_method_id", label: "育种方法", type: "text" },
    ],
    data: [
      { id: "g1", germplasm_name: "B73", genus: "Zea", species: "mays", crop_id: "Maize", breeding_method_id: "Inbred Line" },
      { id: "g2", germplasm_name: "Mo17", genus: "Zea", species: "mays", crop_id: "Maize", breeding_method_id: "Inbred Line" },
    ],
  },
  seedLot: {
    icon: Package,
    iconBg: "bg-gradient-to-br from-lime-500 to-green-600",
    title: "SeedLot 种子批次",
    description: "某批种子或材料库存，记录数量、单位、地点和项目归属",
    addLabel: "新增批次",
    columns: [{ key: "name", label: "批次名称" }, { key: "amount", label: "数量" }, { key: "units", label: "单位" }, { key: "program_id", label: "项目" }, { key: "location_id", label: "地点" }],
    fields: [
      { key: "name", label: "批次名称", type: "text", required: true },
      { key: "amount", label: "数量", type: "number" },
      { key: "units", label: "单位", type: "text", placeholder: "g / seed / kg" },
      { key: "program_id", label: "项目", type: "text" },
      { key: "location_id", label: "存放地点", type: "text" },
    ],
    data: [{ id: "sl1", name: "B73-SL-2026-001", amount: 5000, units: "g", program_id: "MYIP-2026", location_id: "Beijing Research Station" }],
  },
  obsVariable: {
    icon: ClipboardList,
    iconBg: "bg-gradient-to-br from-purple-500 to-indigo-600",
    title: "ObservationVariable 采集变量",
    description: "Trait + Method + Scale 的组合，是田间采集模板字段",
    addLabel: "新增变量",
    columns: [{ key: "name", label: "变量名称" }, { key: "pui", label: "PUI" }, { key: "trait_id", label: "Trait" }, { key: "method_id", label: "Method" }, { key: "scale_id", label: "Scale" }],
    fields: [
      { key: "name", label: "变量名称", type: "text", required: true },
      { key: "pui", label: "PUI", type: "text" },
      { key: "trait_id", label: "Trait", type: "text" },
      { key: "method_id", label: "Method", type: "text" },
      { key: "scale_id", label: "Scale", type: "text" },
      { key: "crop_id", label: "作物", type: "select", options: cropOptions },
    ],
    data: [{ id: "ov1", name: "Plant Height by Ruler in cm", pui: "CO_322:0000994", trait_id: "Plant Height", method_id: "Ruler Measurement", scale_id: "Centimeter" }],
  },
  obsUnit: {
    icon: Database,
    iconBg: "bg-gradient-to-br from-violet-500 to-purple-600",
    title: "ObservationUnit 观测单元",
    description: "被观察对象，通常是 field、block、plot、plant 或 sample",
    addLabel: "新增观测单元",
    columns: [{ key: "observation_unit_name", label: "单元名称" }, { key: "study_id", label: "Study" }, { key: "germplasm_id", label: "材料" }, { key: "level", label: "层级" }, { key: "row", label: "行" }, { key: "col", label: "列" }],
    fields: [
      { key: "observation_unit_name", label: "单元名称", type: "text", required: true },
      { key: "study_id", label: "Study", type: "text" },
      { key: "germplasm_id", label: "材料", type: "text" },
      { key: "level", label: "层级", type: "select", options: ["field", "block", "plot", "plant", "sample"].map((value) => ({ value, label: value })) },
      { key: "row", label: "行", type: "number" },
      { key: "col", label: "列", type: "number" },
    ],
    data: [{ id: "ou1", observation_unit_name: "Plot-001", study_id: "2026 Beijing Yield Trial", germplasm_id: "B73", level: "plot", row: 1, col: 1 }],
  },
  observation: {
    icon: TrendingUp,
    iconBg: "bg-gradient-to-br from-violet-500 to-fuchsia-600",
    title: "Observation 观测值",
    description: "某个 ObservationUnit 上某个 ObservationVariable 的实际测量值",
    addLabel: "新增观测值",
    columns: [{ key: "observation_unit_id", label: "观测单元" }, { key: "observation_variable_id", label: "变量" }, { key: "value", label: "值" }, { key: "collector", label: "采集人" }, { key: "observation_time_stamp", label: "采集时间" }],
    fields: [
      { key: "observation_unit_id", label: "观测单元", type: "text", required: true },
      { key: "observation_variable_id", label: "变量", type: "text", required: true },
      { key: "value", label: "值", type: "text", required: true },
      { key: "collector", label: "采集人", type: "text" },
      { key: "observation_time_stamp", label: "采集时间", type: "date" },
    ],
    data: [{ id: "obs1", observation_unit_id: "Plot-001", observation_variable_id: "Plant Height", value: "168", collector: "Zhang Ming", observation_time_stamp: "2026-06-12" }],
  },
  eventImage: {
    icon: Camera,
    iconBg: "bg-gradient-to-br from-fuchsia-500 to-pink-600",
    title: "Event / Image 事件和图像",
    description: "记录播种、施肥、灌溉、病虫害处理，以及田间图片资料",
    addLabel: "新增事件/图像",
    columns: [{ key: "event_type", label: "事件类型" }, { key: "event_description", label: "说明" }, { key: "study_id", label: "Study" }, { key: "imageurl", label: "图像地址" }],
    fields: [
      { key: "event_type", label: "事件类型", type: "text", required: true },
      { key: "study_id", label: "Study", type: "text" },
      { key: "imageurl", label: "图像地址", type: "text" },
      { key: "event_description", label: "事件说明", type: "textarea", colSpan: 2 },
    ],
    data: [{ id: "ev1", event_type: "Irrigation", event_description: "滴灌 2 小时", study_id: "2026 Beijing Yield Trial", imageurl: "-" }],
  },
  samplePlate: {
    icon: TestTube,
    iconBg: "bg-gradient-to-br from-rose-500 to-red-600",
    title: "Sample / Plate 样品管理",
    description: "从观测单元采样、排板，并准备送检或测序",
    addLabel: "新增样品",
    columns: [{ key: "sample_name", label: "样品名称" }, { key: "sample_barcode", label: "条码" }, { key: "sample_type", label: "类型" }, { key: "tissue_type", label: "组织" }, { key: "plate_id", label: "板号" }],
    fields: [
      { key: "sample_name", label: "样品名称", type: "text", required: true },
      { key: "sample_barcode", label: "样品条码", type: "text" },
      { key: "sample_type", label: "样品类型", type: "text" },
      { key: "tissue_type", label: "组织类型", type: "text" },
      { key: "observation_unit_id", label: "观测单元", type: "text" },
      { key: "plate_id", label: "板号", type: "text" },
    ],
    data: [{ id: "s1", sample_name: "DNA-B73-001", sample_barcode: "SMP0001", sample_type: "DNA", tissue_type: "Leaf", plate_id: "P96-001" }],
  },
  variant: {
    icon: Sigma,
    iconBg: "bg-gradient-to-br from-rose-600 to-pink-700",
    title: "Reference / Variant / Call 变异数据",
    description: "维护参考基因组、变异集合、位点和 genotype call 结果",
    addLabel: "新增变异记录",
    columns: [{ key: "variant_name", label: "变异名称" }, { key: "variant_type", label: "类型" }, { key: "reference_set_id", label: "参考集合" }, { key: "variant_start", label: "起点" }, { key: "genotype", label: "Genotype" }],
    fields: [
      { key: "variant_name", label: "变异名称", type: "text", required: true },
      { key: "variant_type", label: "变异类型", type: "select", options: ["SNP", "INDEL", "SV"].map((value) => ({ value, label: value })) },
      { key: "reference_set_id", label: "参考集合", type: "text" },
      { key: "variant_start", label: "起点", type: "number" },
      { key: "genotype", label: "Genotype", type: "text" },
    ],
    data: [{ id: "v1", variant_name: "S1_12345_A_T", variant_type: "SNP", reference_set_id: "B73Ref5", variant_start: 12345, genotype: "A/T" }],
  },
};

brapiEntityConfigs.traitMethodScale = {
  icon: Ruler,
  iconBg: "bg-gradient-to-br from-orange-500 to-amber-600",
  title: "Trait / Method / Scale",
  description: "配置测什么、怎么测、用什么单位，最终组合为 ObservationVariable",
  addLabel: "新增标准项",
  columns: [{ key: "kind", label: "类型" }, { key: "name", label: "名称" }, { key: "ontology_id", label: "本体" }, { key: "description", label: "说明" }, { key: "units", label: "单位" }],
  fields: [
    { key: "kind", label: "类型", type: "select", required: true, options: ["Trait", "Method", "Scale"].map((value) => ({ value, label: value })) },
    { key: "name", label: "名称", type: "text", required: true },
    { key: "ontology_id", label: "本体", type: "text" },
    { key: "units", label: "单位", type: "text" },
    { key: "description", label: "说明", type: "textarea", colSpan: 2 },
  ],
  data: [
    { id: "t1", kind: "Trait", name: "Plant Height", ontology_id: "CO_322", description: "植株高度", units: "-" },
    { id: "m1", kind: "Method", name: "Ruler Measurement", ontology_id: "CO_322", description: "直尺测量", units: "-" },
    { id: "s1", kind: "Scale", name: "Centimeter", ontology_id: "CO_322", description: "厘米", units: "cm" },
  ],
};

brapiEntityConfigs.crossPedigree = {
  icon: GitFork,
  iconBg: "bg-gradient-to-br from-green-600 to-emerald-700",
  title: "Cross / Pedigree 杂交与系谱",
  description: "维护 CrossingProject、CrossEntity、CrossParent 和材料亲缘关系",
  addLabel: "新增杂交记录",
  columns: [{ key: "name", label: "杂交名称" }, { key: "crossing_project_id", label: "杂交项目" }, { key: "female_parent", label: "母本" }, { key: "male_parent", label: "父本" }, { key: "status", label: "状态" }],
  fields: [
    { key: "name", label: "杂交名称", type: "text", required: true },
    { key: "crossing_project_id", label: "杂交项目", type: "text" },
    { key: "female_parent", label: "母本", type: "text" },
    { key: "male_parent", label: "父本", type: "text" },
    { key: "status", label: "状态", type: "select", options: ["planned", "completed", "failed"].map((value) => ({ value, label: value })) },
  ],
  data: [{ id: "c1", name: "B73 x Mo17", crossing_project_id: "MYIP-CROSS-2026", female_parent: "B73", male_parent: "Mo17", status: "planned" }],
};


