"use client";

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

export type FactType = "world" | "experience" | "observation";

export const ALL_FACT_TYPES: FactType[] = ["world", "experience", "observation"];

const FACT_TYPE_CONFIG: Record<
  FactType,
  { label: string; active: string; inactive: string; dot: string }
> = {
  world: {
    label: "World",
    active: "bg-blue-500/15 text-blue-700 border-blue-400 dark:text-blue-300 dark:border-blue-500",
    inactive:
      "border-border text-muted-foreground hover:border-blue-300 hover:text-blue-600 dark:hover:text-blue-400",
    dot: "bg-blue-500",
  },
  experience: {
    label: "Experience",
    active:
      "bg-emerald-500/15 text-emerald-700 border-emerald-400 dark:text-emerald-300 dark:border-emerald-500",
    inactive:
      "border-border text-muted-foreground hover:border-emerald-300 hover:text-emerald-600 dark:hover:text-emerald-400",
    dot: "bg-emerald-500",
  },
  observation: {
    label: "Observation",
    active:
      "bg-amber-500/15 text-amber-700 border-amber-400 dark:text-amber-300 dark:border-amber-500",
    inactive:
      "border-border text-muted-foreground hover:border-amber-300 hover:text-amber-600 dark:hover:text-amber-400",
    dot: "bg-amber-500",
  },
};

function FactTypePill({
  ft,
  active,
  onToggle,
}: {
  ft: FactType;
  active: boolean;
  onToggle: () => void;
}) {
  const cfg = FACT_TYPE_CONFIG[ft];
  return (
    <button
      type="button"
      onClick={onToggle}
      className={cn(
        "inline-flex items-center gap-1.5 rounded-full border px-3 py-1 text-xs font-medium transition-all",
        active ? cfg.active : cfg.inactive
      )}
    >
      <span
        className={cn("h-1.5 w-1.5 rounded-full", active ? cfg.dot : "bg-muted-foreground/50")}
      />
      {cfg.label}
    </button>
  );
}

/**
 * Inline pill-toggle fact-type filter for filter bars.
 * An empty selection means "all types included".
 */
export function FactTypeFilter({
  value,
  onChange,
  label = "Fact types:",
}: {
  value: FactType[];
  onChange: (next: FactType[]) => void;
  label?: string;
}) {
  const toggle = (ft: FactType) =>
    onChange(value.includes(ft) ? value.filter((f) => f !== ft) : [...value, ft]);

  return (
    <div className="flex items-center gap-2">
      {label && <span className="text-sm font-medium text-muted-foreground">{label}</span>}
      <div className="flex gap-1.5">
        {ALL_FACT_TYPES.map((ft) => (
          <FactTypePill key={ft} ft={ft} active={value.includes(ft)} onToggle={() => toggle(ft)} />
        ))}
      </div>
    </div>
  );
}

/**
 * Pill-toggle group for use inside forms/dialogs.
 */
export function FactTypeCheckboxGroup({
  value,
  onChange,
}: {
  value: FactType[];
  onChange: (next: FactType[]) => void;
}) {
  const toggle = (ft: FactType) =>
    onChange(value.includes(ft) ? value.filter((f) => f !== ft) : [...value, ft]);

  return (
    <div className="flex flex-wrap gap-1.5">
      {ALL_FACT_TYPES.map((ft) => (
        <FactTypePill key={ft} ft={ft} active={value.includes(ft)} onToggle={() => toggle(ft)} />
      ))}
    </div>
  );
}
