/**
 * Color Utilities
 *
 * Color palette for the Expert Discovery Interface video theme.
 * Based on dark theme from the marketing-web prototype.
 */

export type ColorPalette = {
  // Backgrounds
  background: string;
  surface: string;
  surfaceElevated: string;

  // Text
  text: string;
  textSecondary: string;
  textMuted: string;

  // Brand
  primary: string;
  accent: string;

  // Borders
  border: string;

  // Status
  success: string;
  warning: string;
  error: string;
};

/**
 * Default dark theme palette.
 */
export const darkPalette: ColorPalette = {
  // Backgrounds
  background: '#000000',
  surface: '#0a0a0a',
  surfaceElevated: '#141414',

  // Text
  text: '#ffffff',
  textSecondary: '#a1a1aa',
  textMuted: '#71717a',

  // Brand
  primary: '#3b82f6',
  accent: '#60a5fa',

  // Borders
  border: '#27272a',

  // Status
  success: '#22c55e',
  warning: '#f59e0b',
  error: '#ef4444',
};

/**
 * Generate a palette from a primary color.
 * For now, just uses the default palette with the primary color swapped.
 */
export function generatePalette(primaryColor: string): ColorPalette {
  return {
    ...darkPalette,
    primary: primaryColor,
    accent: lightenColor(primaryColor, 0.2),
  };
}

/**
 * Lighten a hex color by a percentage.
 */
function lightenColor(hex: string, percent: number): string {
  const num = parseInt(hex.replace('#', ''), 16);
  const r = Math.min(255, Math.floor((num >> 16) + (255 - (num >> 16)) * percent));
  const g = Math.min(255, Math.floor(((num >> 8) & 0x00ff) + (255 - ((num >> 8) & 0x00ff)) * percent));
  const b = Math.min(255, Math.floor((num & 0x0000ff) + (255 - (num & 0x0000ff)) * percent));
  return `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, '0')}`;
}

/**
 * Get contrasting text color (white or black) for a background.
 */
export function getContrastColor(hex: string): string {
  const num = parseInt(hex.replace('#', ''), 16);
  const r = num >> 16;
  const g = (num >> 8) & 0x00ff;
  const b = num & 0x0000ff;
  const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
  return luminance > 0.5 ? '#000000' : '#ffffff';
}

/**
 * StemeDB brand palette.
 * Dark theme matching the StemeDB dashboard (zinc-950 background).
 */
export const stemedbPalette: ColorPalette = {
  // Backgrounds (zinc-950, zinc-900, zinc-800)
  background: '#09090b',
  surface: '#18181b',
  surfaceElevated: '#27272a',

  // Text (zinc-50, zinc-400, zinc-500)
  text: '#fafafa',
  textSecondary: '#a1a1aa',
  textMuted: '#71717a',

  // Brand (blue-500, blue-400)
  primary: '#3b82f6',
  accent: '#60a5fa',

  // Borders (zinc-700)
  border: '#3f3f46',

  // Status
  success: '#22c55e',
  warning: '#f59e0b',
  error: '#ef4444',
};
