import type { EntryType } from "@cents/domain";
import {
  Ambulance,
  Apple,
  Baby,
  Banknote,
  BatteryCharging,
  Bike,
  BookOpen,
  BriefcaseBusiness,
  Building2,
  BusFront,
  CarTaxiFront,
  ChartNoAxesCombined,
  Clapperboard,
  ClipboardCheck,
  Coffee,
  CookingPot,
  Cookie,
  Crown,
  CupSoda,
  Droplets,
  Dumbbell,
  FileText,
  Flame,
  Fuel,
  Gamepad2,
  Gauge,
  Gift,
  GraduationCap,
  HandCoins,
  HandHeart,
  Handshake,
  HeartPulse,
  Hospital,
  Hotel,
  House,
  Milk,
  ParkingCircle,
  Pencil,
  Pill,
  Puzzle,
  ReceiptText,
  School,
  ShieldCheck,
  ShoppingBag,
  ShoppingBasket,
  Shirt,
  SoapDispenserDroplet,
  Smartphone,
  Soup,
  Sparkles,
  Stethoscope,
  Syringe,
  Ticket,
  TrainFront,
  Utensils,
  UsersRound,
  WalletCards,
  Zap,
  CircleEllipsis,
  PackageOpen,
  type LucideIcon,
} from "@lucide/vue";

export type Category = {
  id: string;
  label: string;
  color: string;
  tint: string;
  icon: LucideIcon;
  children?: Category[];
};

export type EntryTypeOption = {
  id: EntryType;
  label: string;
  color: string;
  tint: string;
  icon: LucideIcon;
};

export const entryTypes: EntryTypeOption[] = [
  { id: "expense", label: "支出", color: "#ef5b3f", tint: "#fff0ec", icon: ReceiptText },
  { id: "income", label: "收入", color: "#16966f", tint: "#e9faf4", icon: Banknote },
];

export const categories: Record<EntryType, Category[]> = {
  expense: [
    {
      id: "food",
      label: "餐饮",
      color: "#f06a34",
      tint: "#fff0e8",
      icon: Utensils,
      children: [
        { id: "takeout", label: "外卖", color: "#f06a34", tint: "#fff0e8", icon: Soup },
        { id: "restaurant", label: "堂食", color: "#e9572e", tint: "#ffede7", icon: CookingPot },
        { id: "coffee", label: "咖啡", color: "#9b6a48", tint: "#f8eee6", icon: Coffee },
        { id: "snacks", label: "零食", color: "#c98535", tint: "#fff4e5", icon: Cookie },
        { id: "drinks", label: "饮品", color: "#2d8f9e", tint: "#eaf8fa", icon: CupSoda },
        { id: "fruit", label: "水果", color: "#df6345", tint: "#fff0eb", icon: Apple },
      ],
    },
    {
      id: "transport",
      label: "交通",
      color: "#3478e5",
      tint: "#edf4ff",
      icon: BusFront,
      children: [
        { id: "taxi", label: "打车", color: "#3478e5", tint: "#edf4ff", icon: CarTaxiFront },
        { id: "public-transit", label: "公交", color: "#2563c7", tint: "#edf4ff", icon: BusFront },
        { id: "subway", label: "地铁", color: "#5368bd", tint: "#eef1fb", icon: TrainFront },
        { id: "train", label: "火车", color: "#4869b1", tint: "#eef2fb", icon: TrainFront },
        { id: "cycling", label: "骑行", color: "#168c87", tint: "#e8f8f6", icon: Bike },
        { id: "fuel", label: "加油", color: "#6f6b58", tint: "#f3f2ed", icon: Fuel },
        { id: "charging", label: "充电", color: "#2b8a74", tint: "#e8f7f2", icon: BatteryCharging },
        { id: "parking", label: "停车", color: "#4f7190", tint: "#edf3f7", icon: ParkingCircle },
        { id: "highway", label: "高速", color: "#697970", tint: "#f0f4f2", icon: Gauge },
      ],
    },
    {
      id: "shopping",
      label: "购物",
      color: "#d84486",
      tint: "#fff0f7",
      icon: ShoppingBag,
      children: [
        { id: "clothing", label: "服饰", color: "#b14e79", tint: "#faeef4", icon: Shirt },
        { id: "shopping-food", label: "食品", color: "#df6345", tint: "#fff0eb", icon: Apple },
        { id: "groceries", label: "杂货", color: "#1a9b62", tint: "#eaf9f1", icon: ShoppingBasket },
        { id: "digital", label: "数码", color: "#52606f", tint: "#eff2f5", icon: Smartphone },
        { id: "home", label: "家居", color: "#2b8a74", tint: "#e8f7f2", icon: House },
        { id: "toys", label: "玩具", color: "#7559d9", tint: "#f2efff", icon: Puzzle },
      ],
    },
    {
      id: "parenting",
      label: "育儿",
      color: "#de6b79",
      tint: "#fff0f2",
      icon: Baby,
      children: [
        { id: "formula", label: "奶粉", color: "#c88945", tint: "#fff5e8", icon: Milk },
        { id: "maternal-baby", label: "母婴", color: "#de6b79", tint: "#fff0f2", icon: Baby },
        { id: "children-clothing", label: "童装", color: "#b14e79", tint: "#faeef4", icon: Shirt },
        { id: "children-toys", label: "玩具", color: "#7559d9", tint: "#f2efff", icon: Puzzle },
        { id: "children-tuition", label: "学费", color: "#4869b1", tint: "#edf2fb", icon: GraduationCap },
        { id: "children-supplements", label: "补剂", color: "#2f8768", tint: "#eaf7f1", icon: Pill },
        { id: "children-care", label: "洗护", color: "#2d8f9e", tint: "#eaf8fa", icon: SoapDispenserDroplet },
        { id: "children-paper", label: "纸品", color: "#7a8a86", tint: "#f0f4f2", icon: PackageOpen },
        { id: "children-other", label: "其他", color: "#526a78", tint: "#edf2f4", icon: CircleEllipsis },
      ],
    },
    {
      id: "entertainment",
      label: "娱乐",
      color: "#7958b3",
      tint: "#f3effb",
      icon: Gamepad2,
      children: [
        { id: "games", label: "游戏", color: "#7958b3", tint: "#f3effb", icon: Gamepad2 },
        { id: "movies", label: "电影", color: "#a74d65", tint: "#faeef2", icon: Clapperboard },
        { id: "massage", label: "按摩", color: "#9b678f", tint: "#f8eef5", icon: Sparkles },
        { id: "sports", label: "运动", color: "#2f8768", tint: "#eaf7f1", icon: Dumbbell },
        { id: "tickets", label: "门票", color: "#b57a0e", tint: "#fff7dd", icon: Ticket },
        { id: "hotel", label: "酒店", color: "#526a78", tint: "#edf2f4", icon: Hotel },
      ],
    },
    {
      id: "social",
      label: "社交",
      color: "#c35f76",
      tint: "#faeef2",
      icon: Handshake,
      children: [
        { id: "red-packet", label: "红包", color: "#d94f45", tint: "#fff0ed", icon: WalletCards },
        { id: "gift", label: "礼物", color: "#b57a0e", tint: "#fff7dd", icon: Gift },
        { id: "treating", label: "请客", color: "#d76a35", tint: "#fff1e9", icon: Utensils },
        { id: "gathering", label: "聚会", color: "#5368bd", tint: "#eef1fb", icon: UsersRound },
        { id: "donation", label: "捐赠", color: "#2f8768", tint: "#eaf7f1", icon: HandHeart },
        { id: "social-obligation", label: "人情", color: "#9b6a48", tint: "#f8eee6", icon: HandCoins },
      ],
    },
    {
      id: "health",
      label: "医疗",
      color: "#df3f5f",
      tint: "#fff0f2",
      icon: HeartPulse,
      children: [
        { id: "pharmacy", label: "药店", color: "#2f8768", tint: "#eaf7f1", icon: Pill },
        { id: "outpatient", label: "门诊", color: "#3478a5", tint: "#edf5f9", icon: Stethoscope },
        { id: "emergency", label: "急诊", color: "#d94f45", tint: "#fff0ed", icon: Ambulance },
        { id: "vaccine", label: "疫苗", color: "#7559d9", tint: "#f2efff", icon: Syringe },
        { id: "hospitalization", label: "住院", color: "#526a78", tint: "#edf2f4", icon: Hospital },
      ],
    },
    {
      id: "education",
      label: "教育",
      color: "#4869b1",
      tint: "#edf2fb",
      icon: GraduationCap,
      children: [
        { id: "books", label: "书籍", color: "#6b5c4c", tint: "#f5f0ea", icon: BookOpen },
        { id: "courses", label: "课程", color: "#4869b1", tint: "#edf2fb", icon: School },
        { id: "tuition", label: "学费", color: "#b57a0e", tint: "#fff7dd", icon: GraduationCap },
        { id: "stationery", label: "文具", color: "#d84486", tint: "#fff0f7", icon: Pencil },
        { id: "exams", label: "考试", color: "#526a78", tint: "#edf2f4", icon: ClipboardCheck },
      ],
    },
    {
      id: "bills",
      label: "缴费",
      color: "#526a78",
      tint: "#edf2f4",
      icon: FileText,
      children: [
        { id: "mobile", label: "话费", color: "#526a78", tint: "#edf2f4", icon: Smartphone },
        { id: "water", label: "水费", color: "#2d8f9e", tint: "#eaf8fa", icon: Droplets },
        { id: "electricity", label: "电费", color: "#b27a22", tint: "#fff7e7", icon: Zap },
        { id: "gas", label: "燃气", color: "#d76a35", tint: "#fff1e9", icon: Flame },
        { id: "property-fee", label: "物业", color: "#387b70", tint: "#eaf6f3", icon: Building2 },
        { id: "parking-fee", label: "车位", color: "#4f7190", tint: "#edf3f7", icon: ParkingCircle },
        { id: "membership", label: "会员", color: "#8559cf", tint: "#f4efff", icon: Crown },
        { id: "insurance", label: "保险", color: "#477866", tint: "#edf6f2", icon: ShieldCheck },
      ],
    },
  ],
  income: [
    { id: "salary", label: "工资", color: "#15956d", tint: "#e8faf3", icon: BriefcaseBusiness },
    { id: "bonus", label: "奖金", color: "#b57a0e", tint: "#fff7dd", icon: Gift },
    { id: "part-time", label: "兼职", color: "#3577c9", tint: "#edf5ff", icon: HandCoins },
    { id: "investment", label: "投资", color: "#28805d", tint: "#eaf7f1", icon: ChartNoAxesCombined },
    { id: "other-income", label: "其他", color: "#8559cf", tint: "#f4efff", icon: Banknote },
    { id: "received-red-packet", label: "红包", color: "#d94f45", tint: "#fff0ed", icon: WalletCards },
  ],
};

const categoryAliases: Record<string, string> = {
  daily: "shopping",
};

export function findCategory(categoryId: string): Category | EntryTypeOption | undefined {
  const resolvedId = categoryAliases[categoryId] ?? categoryId;
  const entryType = entryTypes.find((type) => type.id === resolvedId);
  if (entryType) return entryType;
  for (const group of Object.values(categories)) {
    for (const category of group) {
      if (category.id === resolvedId) return category;
      const child = category.children?.find((item) => item.id === resolvedId);
      if (child) return child;
    }
  }
  return undefined;
}

export function findCategoryPath(type: EntryType, categoryId: string): Category[] {
  const resolvedId = categoryAliases[categoryId] ?? categoryId;
  for (const category of categories[type]) {
    if (category.id === resolvedId) return [category];
    const child = category.children?.find((item) => item.id === resolvedId);
    if (child) return [category, child];
  }
  return [];
}
