export type HairStyle = "short" | "spiky" | "side-part" | "curly" | "buzz" | "bob";
export type EyeStyle = "dot" | "line" | "wide";

export interface PawnAppearance {
  hairStyle: HairStyle;
  eyeStyle: EyeStyle;
  skinColor: string;
  hairColor: string;
  shirtColor: string;
  pantsColor: string;
  shoeColor: string;
}

export function hashString(str: string): number {
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0;
  }
  return Math.abs(hash);
}

const HAIR_STYLES: HairStyle[] = ["short", "spiky", "side-part", "curly", "buzz", "bob"];
const EYE_STYLES: EyeStyle[] = ["dot", "line", "wide"];
const SKIN_COLORS = ["#fde2c8", "#f5c5a0", "#d4956b", "#a0714f", "#6b4226", "#ffe0bd"];
const HAIR_COLORS = ["#2c1b0e", "#5a3214", "#c2884a", "#e8c068", "#7b6d66"];

/** Warm, Claude-adjacent wardrobe — terracotta, ochre, sage, teal, plum. */
const SHIRT_COLORS = [
  "#d97757",
  "#c98a2d",
  "#7c9b5f",
  "#3f9d97",
  "#5b8dd9",
  "#9a7fd1",
  "#c2607e",
  "#a8703f",
  "#5f9e64",
  "#b45309",
];
const PANTS_COLORS = ["#4a4238", "#52525b", "#5b4636", "#37475c", "#4a3f63", "#6b3f4a"];
const SHOE_COLORS = ["#3f3f46", "#7c2d12", "#1e293b", "#525252"];

export function generateAppearance(agentId: string): PawnAppearance {
  const h = hashString(agentId);
  const bits = (offset: number, count: number) => (h >>> offset) % count;

  return {
    hairStyle: HAIR_STYLES[bits(3, HAIR_STYLES.length)],
    eyeStyle: EYE_STYLES[bits(6, EYE_STYLES.length)],
    skinColor: SKIN_COLORS[bits(8, SKIN_COLORS.length)],
    hairColor: HAIR_COLORS[bits(11, HAIR_COLORS.length)],
    shirtColor: SHIRT_COLORS[h % SHIRT_COLORS.length],
    pantsColor: PANTS_COLORS[bits(14, PANTS_COLORS.length)],
    shoeColor: SHOE_COLORS[bits(17, SHOE_COLORS.length)],
  };
}

/** Darken a hex color for sleeves / shading. */
export function shade(hex: string, factor: number): string {
  const r = Math.round(parseInt(hex.slice(1, 3), 16) * factor);
  const g = Math.round(parseInt(hex.slice(3, 5), 16) * factor);
  const b = Math.round(parseInt(hex.slice(5, 7), 16) * factor);
  return `rgb(${r},${g},${b})`;
}
