/**
 * Convert hex color codes to friendly color names
 * Customers see names, admins/data uses hex codes internally
 */

const COLOR_NAMES: Record<string, string> = {
  // Neutrals
  '#000000': 'Black',
  '#1a1a1a': 'Charcoal',
  '#333333': 'Dark Gray',
  '#4d4d4d': 'Gray',
  '#666666': 'Medium Gray',
  '#808080': 'Light Gray',
  '#999999': 'Silver',
  '#b3b3b3': 'Pale Gray',
  '#cccccc': 'Very Light Gray',
  '#e6e6e6': 'Off-White',
  '#ffffff': 'White',

  // Reds & Pinks
  '#cc0000': 'Red',
  '#ff0000': 'Bright Red',
  '#ff3333': 'Light Red',
  '#ff6666': 'Coral',
  '#ff9999': 'Salmon',
  '#ffcccc': 'Pink',
  '#ff69b4': 'Hot Pink',
  '#ff1493': 'Deep Pink',
  '#c71585': 'Crimson',
  '#8b0000': 'Dark Red',

  // Oranges & Browns
  '#ff6600': 'Orange',
  '#ff8c00': 'Dark Orange',
  '#ffaa00': 'Burnt Orange',
  '#ff9900': 'Apricot',
  '#ffbb99': 'Peach',
  '#cc6600': 'Rust',
  '#996633': 'Brown',
  '#8b4513': 'Saddle Brown',
  '#a0522d': 'Sienna',
  '#d2691e': 'Chocolate',

  // Yellows & Golds
  '#ffff00': 'Yellow',
  '#ffff99': 'Light Yellow',
  '#ffcc00': 'Gold',
  '#ffd700': 'Bright Gold',
  '#f0e68c': 'Khaki',
  '#f4a460': 'Sandy Brown',
  '#daa520': 'Goldenrod',

  // Greens
  '#00aa00': 'Green',
  '#00cc00': 'Lime',
  '#66ff66': 'Light Green',
  '#99ff99': 'Pale Green',
  '#33cc33': 'Kelly Green',
  '#228b22': 'Forest Green',
  '#006600': 'Dark Green',
  '#355c3d': 'Hunter Green',
  '#2f4f2f': 'Dark Forest Green',
  '#3cb371': 'Medium Sea Green',
  '#20b2aa': 'Light Sea Green',
  '#008080': 'Teal',
  '#006666': 'Dark Teal',

  // Blues
  '#0066ff': 'Blue',
  '#0099cc': 'Sky Blue',
  '#00ccff': 'Cyan',
  '#3399ff': 'Light Blue',
  '#6699ff': 'Periwinkle',
  '#9999ff': 'Lavender Blue',
  '#1e90ff': 'Dodger Blue',
  '#4169e1': 'Royal Blue',
  '#00008b': 'Dark Blue',
  '#000080': 'Navy',
  '#191970': 'Midnight Blue',

  // Purples & Violets
  '#9933ff': 'Purple',
  '#cc00ff': 'Magenta',
  '#ff00ff': 'Fuchsia',
  '#ee82ee': 'Violet',
  '#9370db': 'Medium Purple',
  '#8b008b': 'Dark Magenta',
  '#4b0082': 'Indigo',
  '#663399': 'Rebecca Purple',

  // Common craft colors
  '#17181c': 'Graphite',
  '#1ea7e0': 'Turquoise',
  '#fbe9e7': 'Blush',
  '#f5f5f5': 'Smoke',
};

function hexDistance(a: string, b: string): number {
  const [ar, ag, ab] = [1, 3, 5].map((i) => parseInt(a.slice(i, i + 2), 16));
  const [br, bg, bb] = [1, 3, 5].map((i) => parseInt(b.slice(i, i + 2), 16));
  return Math.sqrt((ar - br) ** 2 + (ag - bg) ** 2 + (ab - bb) ** 2);
}

/**
 * Get the friendly name for a hex color
 * Matches exactly if available, otherwise finds the nearest color in the palette
 */
export function getColorName(hex: string): string {
  const normalized = hex.toLowerCase();
  if (COLOR_NAMES[normalized]) return COLOR_NAMES[normalized];

  // Find nearest color by distance (tolerance for lighting/JPEG compression variations)
  let nearest: string | null = null;
  let minDistance = Infinity;
  for (const [colorHex, colorName] of Object.entries(COLOR_NAMES)) {
    const dist = hexDistance(normalized, colorHex);
    if (dist < minDistance && dist < 40) {
      minDistance = dist;
      nearest = colorName;
    }
  }
  return nearest || 'Custom';
}

/**
 * Get the hex code from a color name
 * Used for reverse lookup if needed
 */
export function getColorHex(name: string): string | null {
  for (const [hex, colorName] of Object.entries(COLOR_NAMES)) {
    if (colorName.toLowerCase() === name.toLowerCase()) {
      return hex;
    }
  }
  return null;
}

/**
 * Check if a hex code is in our standard palette
 */
export function isStandardColor(hex: string): boolean {
  return hex.toLowerCase() in COLOR_NAMES;
}

/**
 * Get all available standard color names
 */
export function getAllColorNames(): string[] {
  return Object.values(COLOR_NAMES).sort();
}
