import type { LucideIcon } from "lucide-react";
import {
  // Business
  Building2, Briefcase, Landmark, Store, Factory, Warehouse, Building, Hotel, Castle, Church,
  // People
  Users, User, UserCheck, UserPlus, UserCog, Contact, PersonStanding, Baby, HeartHandshake, Handshake,
  // Finance
  Wallet, CreditCard, Banknote, Coins, PiggyBank, Receipt, Calculator, BarChart3, ChartLine, TrendingUp,
  // Documents
  FileText, FileCheck, Files, Clipboard, Notebook, BookOpen, BookMarked, Newspaper, Scroll, Archive,
  // Tech
  Database, Server, Cloud, Monitor, Laptop, Smartphone, Cpu, HardDrive, Wifi, Globe,
  // Communication
  Mail, MessageSquare, Phone, Headphones, Megaphone, Bell, Radio, Video, Podcast, Rss,
  // Security
  Shield, Lock, Key, Eye, Fingerprint, Scan, ShieldCheck, LockKeyhole, BadgeCheck, Verified,
  // Commerce
  ShoppingCart, Package, Tag, Gift, Truck, Box, Barcode, ShoppingBag, Ticket, Percent,
  // Tools
  Wrench, Settings, Cog, Hammer, Paintbrush, Ruler, Scissors, Compass, Thermometer, Gauge,
  // General
  Home, Zap, Star, Heart, Flag, Target, Rocket, Lightbulb, Award, Crown,
} from "lucide-react";

export interface IconOption {
  name: string;
  icon: LucideIcon;
  category: string;
}

export const ICON_OPTIONS: IconOption[] = [
  // Business
  { name: "building-2", icon: Building2, category: "Business" },
  { name: "briefcase", icon: Briefcase, category: "Business" },
  { name: "landmark", icon: Landmark, category: "Business" },
  { name: "store", icon: Store, category: "Business" },
  { name: "factory", icon: Factory, category: "Business" },
  { name: "warehouse", icon: Warehouse, category: "Business" },
  { name: "building", icon: Building, category: "Business" },
  { name: "hotel", icon: Hotel, category: "Business" },
  { name: "castle", icon: Castle, category: "Business" },
  { name: "church", icon: Church, category: "Business" },
  // People
  { name: "users", icon: Users, category: "People" },
  { name: "user", icon: User, category: "People" },
  { name: "user-check", icon: UserCheck, category: "People" },
  { name: "user-plus", icon: UserPlus, category: "People" },
  { name: "user-cog", icon: UserCog, category: "People" },
  { name: "contact", icon: Contact, category: "People" },
  { name: "person-standing", icon: PersonStanding, category: "People" },
  { name: "baby", icon: Baby, category: "People" },
  { name: "heart-handshake", icon: HeartHandshake, category: "People" },
  { name: "handshake", icon: Handshake, category: "People" },
  // Finance
  { name: "wallet", icon: Wallet, category: "Finance" },
  { name: "credit-card", icon: CreditCard, category: "Finance" },
  { name: "banknote", icon: Banknote, category: "Finance" },
  { name: "coins", icon: Coins, category: "Finance" },
  { name: "piggy-bank", icon: PiggyBank, category: "Finance" },
  { name: "receipt", icon: Receipt, category: "Finance" },
  { name: "calculator", icon: Calculator, category: "Finance" },
  { name: "chart-bar", icon: BarChart3, category: "Finance" },
  { name: "chart-line", icon: ChartLine, category: "Finance" },
  { name: "trending-up", icon: TrendingUp, category: "Finance" },
  // Documents
  { name: "file-text", icon: FileText, category: "Documents" },
  { name: "file-check", icon: FileCheck, category: "Documents" },
  { name: "files", icon: Files, category: "Documents" },
  { name: "clipboard", icon: Clipboard, category: "Documents" },
  { name: "notebook", icon: Notebook, category: "Documents" },
  { name: "book-open", icon: BookOpen, category: "Documents" },
  { name: "book-marked", icon: BookMarked, category: "Documents" },
  { name: "newspaper", icon: Newspaper, category: "Documents" },
  { name: "scroll", icon: Scroll, category: "Documents" },
  { name: "archive", icon: Archive, category: "Documents" },
  // Tech
  { name: "database", icon: Database, category: "Tech" },
  { name: "server", icon: Server, category: "Tech" },
  { name: "cloud", icon: Cloud, category: "Tech" },
  { name: "monitor", icon: Monitor, category: "Tech" },
  { name: "laptop", icon: Laptop, category: "Tech" },
  { name: "smartphone", icon: Smartphone, category: "Tech" },
  { name: "cpu", icon: Cpu, category: "Tech" },
  { name: "hard-drive", icon: HardDrive, category: "Tech" },
  { name: "wifi", icon: Wifi, category: "Tech" },
  { name: "globe", icon: Globe, category: "Tech" },
  // Communication
  { name: "mail", icon: Mail, category: "Communication" },
  { name: "message-square", icon: MessageSquare, category: "Communication" },
  { name: "phone", icon: Phone, category: "Communication" },
  { name: "headphones", icon: Headphones, category: "Communication" },
  { name: "megaphone", icon: Megaphone, category: "Communication" },
  { name: "bell", icon: Bell, category: "Communication" },
  { name: "radio", icon: Radio, category: "Communication" },
  { name: "video", icon: Video, category: "Communication" },
  { name: "podcast", icon: Podcast, category: "Communication" },
  { name: "rss", icon: Rss, category: "Communication" },
  // Security
  { name: "shield", icon: Shield, category: "Security" },
  { name: "lock", icon: Lock, category: "Security" },
  { name: "key", icon: Key, category: "Security" },
  { name: "eye", icon: Eye, category: "Security" },
  { name: "fingerprint", icon: Fingerprint, category: "Security" },
  { name: "scan", icon: Scan, category: "Security" },
  { name: "shield-check", icon: ShieldCheck, category: "Security" },
  { name: "lock-keyhole", icon: LockKeyhole, category: "Security" },
  { name: "badge-check", icon: BadgeCheck, category: "Security" },
  { name: "verified", icon: Verified, category: "Security" },
  // Commerce
  { name: "shopping-cart", icon: ShoppingCart, category: "Commerce" },
  { name: "package", icon: Package, category: "Commerce" },
  { name: "tag", icon: Tag, category: "Commerce" },
  { name: "gift", icon: Gift, category: "Commerce" },
  { name: "truck", icon: Truck, category: "Commerce" },
  { name: "box", icon: Box, category: "Commerce" },
  { name: "barcode", icon: Barcode, category: "Commerce" },
  { name: "shopping-bag", icon: ShoppingBag, category: "Commerce" },
  { name: "ticket", icon: Ticket, category: "Commerce" },
  { name: "percent", icon: Percent, category: "Commerce" },
  // Tools
  { name: "wrench", icon: Wrench, category: "Tools" },
  { name: "settings", icon: Settings, category: "Tools" },
  { name: "cog", icon: Cog, category: "Tools" },
  { name: "hammer", icon: Hammer, category: "Tools" },
  { name: "paintbrush", icon: Paintbrush, category: "Tools" },
  { name: "ruler", icon: Ruler, category: "Tools" },
  { name: "scissors", icon: Scissors, category: "Tools" },
  { name: "compass", icon: Compass, category: "Tools" },
  { name: "thermometer", icon: Thermometer, category: "Tools" },
  { name: "gauge", icon: Gauge, category: "Tools" },
  // General
  { name: "home", icon: Home, category: "General" },
  { name: "zap", icon: Zap, category: "General" },
  { name: "star", icon: Star, category: "General" },
  { name: "heart", icon: Heart, category: "General" },
  { name: "flag", icon: Flag, category: "General" },
  { name: "target", icon: Target, category: "General" },
  { name: "rocket", icon: Rocket, category: "General" },
  { name: "lightbulb", icon: Lightbulb, category: "General" },
  { name: "award", icon: Award, category: "General" },
  { name: "crown", icon: Crown, category: "General" },
];

// Build a quick lookup map
const ICON_MAP = new Map(ICON_OPTIONS.map((o) => [o.name, o.icon]));

export function getIconComponent(name: string | null | undefined): LucideIcon {
  if (!name) return Package;
  return ICON_MAP.get(name) || Package;
}

// Color presets
export interface ColorPreset {
  name: string;
  bg: string;
  text: string;
  border: string;
  dot: string; // solid color for the picker dot
}

export const COLOR_PRESETS: ColorPreset[] = [
  { name: "blue",    bg: "bg-blue-50",    text: "text-blue-600",    border: "border-blue-200",    dot: "bg-blue-500" },
  { name: "violet",  bg: "bg-violet-50",  text: "text-violet-600",  border: "border-violet-200",  dot: "bg-violet-500" },
  { name: "emerald", bg: "bg-emerald-50", text: "text-emerald-600", border: "border-emerald-200", dot: "bg-emerald-500" },
  { name: "orange",  bg: "bg-orange-50",  text: "text-orange-600",  border: "border-orange-200",  dot: "bg-orange-500" },
  { name: "pink",    bg: "bg-pink-50",    text: "text-pink-600",    border: "border-pink-200",    dot: "bg-pink-500" },
  { name: "cyan",    bg: "bg-cyan-50",    text: "text-cyan-600",    border: "border-cyan-200",    dot: "bg-cyan-500" },
  { name: "amber",   bg: "bg-amber-50",   text: "text-amber-600",   border: "border-amber-200",   dot: "bg-amber-500" },
  { name: "red",     bg: "bg-red-50",     text: "text-red-600",     border: "border-red-200",     dot: "bg-red-500" },
  { name: "indigo",  bg: "bg-indigo-50",  text: "text-indigo-600",  border: "border-indigo-200",  dot: "bg-indigo-500" },
  { name: "teal",    bg: "bg-teal-50",    text: "text-teal-600",    border: "border-teal-200",    dot: "bg-teal-500" },
];

const COLOR_MAP = new Map(COLOR_PRESETS.map((c) => [c.name, c]));
const DEFAULT_COLOR: ColorPreset = { name: "gray", bg: "bg-gray-50", text: "text-gray-600", border: "border-gray-200", dot: "bg-gray-500" };

/** Check if a string is a hex color (e.g. "#ff5500" or "ff5500") */
export function isCustomColor(name: string | null | undefined): boolean {
  if (!name) return false;
  return name.startsWith("#") || /^[0-9a-fA-F]{6}$/.test(name);
}

/** Normalize hex — ensure it starts with # */
function normalizeHex(hex: string): string {
  return hex.startsWith("#") ? hex : `#${hex}`;
}

/** Build a ColorPreset from a custom hex value — uses inline styles instead of Tailwind classes */
export function customColorPreset(hex: string): ColorPreset & { custom: true; hex: string } {
  const h = normalizeHex(hex);
  return {
    name: h,
    bg: "",       // use inline style instead
    text: "",     // use inline style instead
    border: "",   // use inline style instead
    dot: "",      // use inline style instead
    custom: true,
    hex: h,
  };
}

export function getColorPreset(name: string | null | undefined): ColorPreset & { custom?: boolean; hex?: string } {
  if (!name) return DEFAULT_COLOR;
  if (isCustomColor(name)) return customColorPreset(name);
  return COLOR_MAP.get(name) || DEFAULT_COLOR;
}
