"use client";

import { CheckCircle2 } from "lucide-react";
import { cn } from "@/lib/utils";

export type BlogCategorySelectionOption = {
  uuid: string;
  title: string;
};

type BlogCategorySelectionProps = {
  categories: BlogCategorySelectionOption[];
  selectedUuids: string[];
  onToggle: (uuid: string) => void;
  emptyText?: string;
};

export function BlogCategorySelection({
  categories,
  selectedUuids,
  onToggle,
  emptyText = "No blog categories available.",
}: BlogCategorySelectionProps) {
  if (!categories.length) {
    return (
      <div className="rounded-2xl border border-dashed border-[var(--theme-border-soft)] bg-[var(--theme-surface)] px-4 py-8 text-center text-sm font-semibold text-muted-foreground">
        {emptyText}
      </div>
    );
  }

  return (
    <div className="grid gap-3 sm:grid-cols-2 xl:grid-cols-3">
      {categories.map((category) => {
        const selected = selectedUuids.includes(category.uuid);

        return (
          <button
            key={category.uuid}
            type="button"
            onClick={() => onToggle(category.uuid)}
            className={cn(
              "rounded-2xl border p-4 text-left text-sm font-bold transition",
              selected
                ? "border-[var(--theme-secondary)] bg-[var(--theme-surface-strong)] text-[var(--theme-emphasis-text)] shadow-sm"
                : "border-[var(--theme-border)] bg-white text-foreground hover:border-[var(--theme-secondary)]/60",
            )}
          >
            <span className="flex items-center gap-3">
              <span
                className={cn(
                  "flex h-5 w-5 items-center justify-center rounded-full border",
                  selected
                    ? "border-[var(--theme-primary)] bg-[var(--theme-primary)] text-white"
                    : "border-[var(--theme-border)] bg-[var(--theme-surface)]",
                )}
              >
                {selected && <CheckCircle2 className="h-3.5 w-3.5" />}
              </span>
              {category.title}
            </span>
          </button>
        );
      })}
    </div>
  );
}
