export type AdminCategory = string;

export type AdminColorWay = {
  name: string;
  swatches: [string, string];
  images: string[];
};

/** Combinaciones OT listas para aplicar (nombre + bolita diagonal). */
export const OT_COLOR_PRESETS: {
  name: string;
  swatches: [string, string];
}[] = [
  { name: "White Court", swatches: ["#FFFFFF", "#1A237E"] },
  { name: "Classic Red", swatches: ["#B71C1C", "#1A237E"] },
  { name: "Navy Heritage", swatches: ["#1A237E", "#FFFFFF"] },
  { name: "Burgundy & White", swatches: ["#6B1E2E", "#FFFFFF"] },
  { name: "White & Tennis Green", swatches: ["#FFFFFF", "#0F382A"] },
  { name: "Verde Tenis & Blanco", swatches: ["#0F382A", "#FFFFFF"] },
  { name: "Azul Marino & Blanco", swatches: ["#1A237E", "#F2EFE9"] },
  { name: "Negro & Blanco", swatches: ["#141414", "#FFFFFF"] },
  { name: "Beige Arena & Blanco", swatches: ["#C4B59A", "#FFFFFF"] },
  { name: "Blanco & Verde Salvia", swatches: ["#F2EFE9", "#9CAF88"] },
  { name: "Vino Tinto & Blanco", swatches: ["#7A1F2B", "#F2EFE9"] },
  { name: "Blanco & Rojo / Marino", swatches: ["#FFFFFF", "#B71C1C"] },
  { name: "Dorado Club", swatches: ["#A68B5B", "#0F382A"] },
  { name: "Crema & Verde", swatches: ["#F3EFE5", "#0F382A"] },
];

/** Hex sueltos para afinar cada mitad de la bolita. */
export const OT_SWATCH_PALETTE = [
  "#FFFFFF",
  "#F3EFE5",
  "#F2EFE9",
  "#C4B59A",
  "#A68B5B",
  "#9CAF88",
  "#0F382A",
  "#002D20",
  "#1A237E",
  "#B71C1C",
  "#6B1E2E",
  "#7A1F2B",
  "#141414",
  "#8A8680",
] as const;

export type AdminSpec = { label: string; value: string };

export type AdminProduct = {
  id: string;
  title: string;
  handle: string;
  description: string;
  status: string;
  thumbnail: string | null;
  images: string[];
  category: string;
  collection: string;
  tagline: string;
  price: number;
  colors: string[];
  sizes: string[];
  color_ways: AdminColorWay[];
  specs: AdminSpec[];
  variant_count: number;
  created_at?: string;
  updated_at?: string;
};

export const CATEGORY_OPTIONS: {
  id: string;
  label: string;
  hint: string;
  sizes: string[];
  collection: string;
}[] = [
  {
    id: "mujer",
    label: "Mujer",
    hint: "Vestidos, enterizos, polos, faldas…",
    sizes: ["XS", "S", "M", "L"],
    collection: "Sporty Elegance",
  },
  {
    id: "hombre",
    label: "Hombre",
    hint: "Polos, bermuda, chaquetas…",
    sizes: ["S", "M", "L", "XL"],
    collection: "Sporty Elegance",
  },
  {
    id: "accesorios",
    label: "Accesorios",
    hint: "Gorra, visor, bolso, packaging…",
    sizes: ["Única"],
    collection: "Club 1976",
  },
  {
    id: "sets",
    label: "Colección / Sets",
    hint: "Set completo, set pareja…",
    sizes: ["XS", "S", "M", "L"],
    collection: "Club 1976",
  },
];

/** Id de categoría a partir del nombre (ej. Zapatos → zapatos). */
export function slugifyCategory(input: string) {
  return input
    .toLowerCase()
    .normalize("NFD")
    .replace(/\p{Diacritic}/gu, "")
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/^-|-$/g, "")
    .slice(0, 40);
}

export function parseSizesInput(raw: string): string[] {
  return String(raw || "")
    .split(/[,|/·]+/)
    .map((s) => s.trim())
    .filter(Boolean);
}

export function resolveCategoryMeta(
  categoryId: string,
  opts?: { label?: string; sizes?: string[]; collection?: string }
) {
  const preset = CATEGORY_OPTIONS.find((c) => c.id === categoryId);
  if (preset) return preset;
  return {
    id: categoryId || "otra",
    label: opts?.label || categoryId || "Otra",
    hint: "Categoría personalizada",
    sizes:
      opts?.sizes?.length
        ? opts.sizes
        : ["S", "M", "L", "XL"],
    collection: opts?.collection || "Club 1976",
  };
}

export const DEFAULT_SPECS: AdminSpec[] = [
  { label: "Tejido", value: "Técnico premium" },
  { label: "Logo", value: "OT bordado" },
  { label: "Acabado", value: "Club 1976" },
];

export function formatCopAdmin(value: number) {
  return new Intl.NumberFormat("es-CO", {
    style: "currency",
    currency: "COP",
    maximumFractionDigits: 0,
  }).format(value || 0);
}

/** Miles con punto: 319900 → "319.900" */
export function formatCopInput(value: number) {
  const n = Math.max(0, Math.floor(Number(value) || 0));
  return new Intl.NumberFormat("es-CO", {
    maximumFractionDigits: 0,
  }).format(n);
}

/** Parsea "319.900" / "319900" / "$ 319.900" → número entero COP */
export function parseCopInput(raw: string) {
  const digits = String(raw || "").replace(/\D/g, "");
  if (!digits) return 0;
  return Math.max(0, parseInt(digits, 10) || 0);
}
