/**
 * Colored dots for calendar day: duty (green), unavailable (amber), vacation (blue), events (accent).
 * Ported from webapp calendar day-indicator markup and markers.css.
 *
 * Rounding is position-based (first / last / only child), not by indicator type, so multiple
 * segments form one "pill": only the left and right ends are rounded.
 */

import { cn } from "@/lib/utils";

export interface DayIndicatorsProps {
  /** Number of duty slots this day. */
  dutyCount: number;
  /** Number of unavailable slots. */
  unavailableCount: number;
  /** Number of vacation slots. */
  vacationCount: number;
  /** Whether the day has external calendar events (e.g. holiday). */
  hasEvents: boolean;
  /** When true (e.g. today cell), use darker dots for contrast. */
  isToday?: boolean;
  className?: string;
}

export function DayIndicators({
  dutyCount,
  unavailableCount,
  vacationCount,
  hasEvents,
  isToday = false,
  className,
}: DayIndicatorsProps) {
  const hasAny = dutyCount > 0 || unavailableCount > 0 || vacationCount > 0 || hasEvents;
  if (!hasAny) return null;

  const dotClass = (variant: "duty" | "unavailable" | "vacation" | "events") =>
    cn(
      "min-w-0 flex-1 h-1 max-h-1.5",
      variant === "duty" && "bg-duty",
      variant === "unavailable" && "bg-unavailable",
      variant === "vacation" && "bg-vacation",
      variant === "events" && "bg-accent",
      isToday && variant === "duty" && "bg-[var(--indicator-today-duty)]",
      isToday && variant === "unavailable" && "bg-[var(--indicator-today-unavailable)]",
      isToday && variant === "vacation" && "bg-[var(--indicator-today-vacation)]",
      isToday && variant === "events" && "bg-[var(--indicator-today-events)]"
    );

  return (
    <div
      className={cn(
        "flex w-[65%] justify-center gap-0.5 mt-1.5",
        "[&>:only-child]:h-1.5 [&>:only-child]:min-w-[6px] [&>:only-child]:max-w-[6px] [&>:only-child]:rounded-full",
        "[&>:first-child:not(:only-child)]:rounded-l-[3px]",
        "[&>:last-child:not(:only-child)]:rounded-r-[3px]",
        className
      )}
      aria-hidden
    >
      {dutyCount > 0 && <span className={dotClass("duty")} />}
      {unavailableCount > 0 && <span className={dotClass("unavailable")} />}
      {vacationCount > 0 && <span className={dotClass("vacation")} />}
      {hasEvents && <span className={dotClass("events")} />}
    </div>
  );
}
