import type { LucideIcon } from "lucide-react";
import {
  BarChart3,
  Binary,
  BookCheck,
  BookOpen,
  Briefcase,
  Calendar,
  Camera,
  ClipboardList,
  Database,
  Dna,
  FlaskConical,
  FolderOpen,
  GitFork,
  GitBranch,
  ImageIcon,
  Leaf,
  LayoutDashboard,
  MapPin,
  Network,
  Package,
  Ruler,
  Scale,
  Sigma,
  Sprout,
  Table2,
  TestTube,
  TrendingUp,
  Users,
  Layers,
  List as ListIcon,
  PanelTop,
  CalendarClock,
  Map,
} from "lucide-react";

export interface BrapiNavItem {
  title: string;
  href: string;
  icon: LucideIcon;
}

export interface BrapiNavGroup {
  title: string;
  items: BrapiNavItem[];
}

export interface BrapiNavSection {
  id: string;
  title: string;
  icon: LucideIcon;
  accentClass: string;
  activeClass: string;
  groups: BrapiNavGroup[];
}

export const brapiTopNav: BrapiNavItem[] = [
  { title: "概览 Dashboard", href: "/dashboard", icon: LayoutDashboard },
];

export const brapiNavSections: BrapiNavSection[] = [
  {
    id: "dict",
    title: "农业基础字典",
    icon: BookOpen,
    accentClass: "text-amber-600",
    activeClass: "bg-amber-50 text-amber-700 dark:bg-amber-400/10 dark:text-amber-200",
    groups: [
      {
        title: "基础资料",
        items: [
          { title: "Crop 作物", href: "/basic-dictionary/base/crop", icon: Leaf },
          { title: "Person 人员", href: "/basic-dictionary/base/person", icon: Users },
          { title: "List 列表", href: "/basic-dictionary/base/list", icon: ListIcon },
          { title: "Location 地点", href: "/basic-dictionary/base/location", icon: MapPin },
          { title: "Season 季节", href: "/basic-dictionary/base/season", icon: Calendar },
        ],
      },
      {
        title: "标准本体",
        items: [
          { title: "Ontology 本体", href: "/basic-dictionary/ontology", icon: Network },
          { title: "Trait / Method / Scale", href: "/basic-dictionary/trait-method-scale", icon: Ruler },
        ],
      },
    ],
  },
  {
    id: "project",
    title: "项目配置",
    icon: FolderOpen,
    accentClass: "text-blue-600",
    activeClass: "bg-blue-50 text-blue-700 dark:bg-blue-400/10 dark:text-blue-200",
    groups: [
      { title: "项目管理", items: [{ title: "Program 项目", href: "/project/program", icon: Briefcase }] },
      { title: "试验管理", items: [{ title: "Trial 试验", href: "/project/trial", icon: FlaskConical }] },
      { title: "Study 管理", items: [{ title: "Study 研究", href: "/project/study", icon: BookCheck }] },
    ],
  },
  {
    id: "germplasm",
    title: "种质材料",
    icon: Sprout,
    accentClass: "text-green-600",
    activeClass: "bg-green-50 text-green-700 dark:bg-green-400/10 dark:text-green-200",
    groups: [
      {
        title: "种质库",
        items: [
          { title: "Breeding Method 育种方法", href: "/germplasm/breeding-method", icon: GitBranch },
          { title: "Germplasm 种质", href: "/germplasm/germplasm", icon: Dna },
        ],
      },
      { title: "库存批次", items: [{ title: "SeedLot 种子批次", href: "/germplasm/seed-lot", icon: Package }] },
      { title: "杂交管理", items: [{ title: "Cross / Pedigree", href: "/germplasm/cross-pedigree", icon: GitFork }] },
    ],
  },
  {
    id: "phenotyping",
    title: "表型采集",
    icon: BarChart3,
    accentClass: "text-purple-600",
    activeClass: "bg-purple-50 text-purple-700 dark:bg-purple-400/10 dark:text-purple-200",
    groups: [
      { title: "采集模板", items: [{ title: "ObservationVariable", href: "/phenotyping/observation-variable", icon: ClipboardList }] },
      { title: "试验设计", items: [{ title: "ObservationUnit", href: "/phenotyping/observation-unit", icon: Database }] },
      { title: "数据录入", items: [{ title: "Observation 观测值", href: "/phenotyping/observation", icon: TrendingUp }] },
      { title: "事件和图像", items: [{ title: "Event / Image", href: "/phenotyping/event-image", icon: Camera }] },
    ],
  },
  {
    id: "genotyping",
    title: "基因型管理",
    icon: Dna,
    accentClass: "text-rose-600",
    activeClass: "bg-rose-50 text-rose-700 dark:bg-rose-400/10 dark:text-rose-200",
    groups: [
      { title: "样品管理", items: [{ title: "Sample / Plate", href: "/genotyping/sample-plate", icon: TestTube }] },
      {
        title: "参考基因组",
        items: [{ title: "ReferenceSet / Reference", href: "/genotyping/reference-set", icon: BookOpen }],
      },
      {
        title: "变异数据",
        items: [
          { title: "VariantSet", href: "/genotyping/variant-set", icon: Layers },
          { title: "Variant", href: "/genotyping/variant", icon: Sigma },
          { title: "CallSet / allele_call", href: "/genotyping/call-set", icon: Binary },
        ],
      },
      {
        title: "遗传图谱",
        items: [
          { title: "GenomeMap / LinkageGroup", href: "/genotyping/genome-map", icon: Map },
        ],
      },
    ],
  },
  {
    id: "frontend-components",
    title: "前端组件",
    icon: Layers,
    accentClass: "text-violet-600",
    activeClass: "bg-violet-50 text-violet-700 dark:bg-violet-400/10 dark:text-violet-200",
    groups: [
      {
        title: "组件演示",
        items: [
          { title: "通用表单", href: "/frontend-components/general-form", icon: Table2 },
          { title: "PageHeader", href: "/frontend-components/page-header", icon: PanelTop },
          { title: "时间组件", href: "/frontend-components/date-time-picker", icon: CalendarClock },
          { title: "单位换算", href: "/frontend-components/unit-converter", icon: Scale },
          { title: "图片上传", href: "/frontend-components/image-uploader", icon: ImageIcon },
        ],
      },
    ],
  },
];

export function getBrapiBreadcrumbs(pathname: string) {
  if (pathname === "/dashboard" || pathname === "/") return ["概览"];
  for (const section of brapiNavSections) {
    for (const group of section.groups) {
      const item = group.items.find((entry) => pathname === entry.href || pathname.startsWith(`${entry.href}/`));
      if (item) return [section.title, group.title, item.title];
    }
  }
  return ["BrAPI 数据管理"];
}
