/** Hardcoded fallback — overridden by admin-chosen colors when available */
export const TYPE_DOT_FALLBACK: Record<string, string> = {
  "Exotic Pole Dance": "bg-gold",
  "Pole Dance": "bg-rose-500",
  "Body Plastic": "bg-purple-500",
  "Трюковые комбинации с пилоном": "bg-amber-500",
};

const COLOR_KEY_TO_DOT: Record<string, string> = {
  rose: "bg-rose-500",
  orange: "bg-orange-500",
  amber: "bg-amber-500",
  yellow: "bg-yellow-400",
  lime: "bg-lime-500",
  emerald: "bg-emerald-500",
  teal: "bg-teal-500",
  cyan: "bg-cyan-500",
  sky: "bg-sky-500",
  blue: "bg-blue-500",
  indigo: "bg-indigo-500",
  violet: "bg-violet-500",
  purple: "bg-purple-500",
  fuchsia: "bg-fuchsia-500",
  pink: "bg-pink-500",
  red: "bg-red-500",
};

const FALLBACK_DOTS = [
  "bg-rose-500", "bg-orange-500", "bg-amber-500", "bg-yellow-400",
  "bg-lime-500", "bg-emerald-500", "bg-teal-500", "bg-cyan-500",
  "bg-sky-500", "bg-blue-500", "bg-indigo-500", "bg-violet-500",
  "bg-purple-500", "bg-fuchsia-500", "bg-pink-500", "bg-red-500",
];

/** Build a type→dot map from class items with optional color field */
export function buildTypeDots(
  classItems?: { name: string; color?: string }[]
): Record<string, string> {
  if (!classItems?.length) return TYPE_DOT_FALLBACK;
  const map: Record<string, string> = {};
  const usedSlots = new Set<number>();

  // First pass: explicit colors
  classItems.forEach((item) => {
    if (item.color && COLOR_KEY_TO_DOT[item.color]) {
      map[item.name] = COLOR_KEY_TO_DOT[item.color];
      const idx = FALLBACK_DOTS.indexOf(COLOR_KEY_TO_DOT[item.color]);
      if (idx >= 0) usedSlots.add(idx);
    }
  });

  // Second pass: assign remaining to unused slots
  let nextSlot = 0;
  classItems.forEach((item) => {
    if (map[item.name]) return;
    while (usedSlots.has(nextSlot) && nextSlot < FALLBACK_DOTS.length) nextSlot++;
    map[item.name] = FALLBACK_DOTS[nextSlot % FALLBACK_DOTS.length];
    usedSlots.add(nextSlot);
    nextSlot++;
  });

  return map;
}

export type StatusFilter = "all" | "hasSlots" | "recruiting";
export type TimeFilter = "all" | "morning" | "afternoon" | "evening";

export const TIME_PRESETS: { value: TimeFilter; label: string; range: [number, number] }[] = [
  { value: "morning", label: "Утро", range: [0, 12 * 60] },
  { value: "afternoon", label: "День", range: [12 * 60, 18 * 60] },
  { value: "evening", label: "Вечер", range: [18 * 60, 24 * 60] },
];

/** Parse start time from "HH:MM–HH:MM" to minutes since midnight */
export function startTimeMinutes(time: string): number {
  const start = time.split("–")[0]?.trim() ?? "";
  const [h, m] = start.split(":").map(Number);
  if (isNaN(h) || isNaN(m)) return 0;
  return h * 60 + m;
}

/** Extended class type with location info for cross-location views */
export interface ScheduleClassWithLocation {
  time: string;
  trainer: string;
  type: string;
  level?: string;
  hasSlots?: boolean;
  recruiting?: boolean;
  groupId?: string;
  locationName?: string;
  locationAddress?: string;
}

/** Strip "г. Минск, " prefix from address for compact display */
export function shortAddress(address: string): string {
  return address.replace(/^г\.\s*Минск,?\s*/i, "").trim();
}

export interface ScheduleDayMerged {
  day: string;
  dayShort: string;
  classes: ScheduleClassWithLocation[];
}

export const pillBase =
  "inline-flex items-center gap-1.5 rounded-full px-3 py-1 text-[11px] font-medium transition-all duration-200 cursor-pointer whitespace-nowrap";
export const pillActive =
  "bg-gold/20 text-gold-dark border border-gold/40 dark:text-gold-light dark:border-gold/30";
export const pillInactive =
  "border border-neutral-200 text-neutral-500 hover:border-neutral-300 dark:border-white/[0.08] dark:text-white/35 dark:hover:border-white/15";
