"use client";

import { TEMPLATES } from "@/lib/templates";
import type { TemplateId } from "@/lib/types";
import { cn } from "@/lib/utils";

export function TemplatePicker({
  value,
  onChange,
  className,
}: {
  value: TemplateId;
  onChange: (id: TemplateId) => void;
  className?: string;
}) {
  return (
    <div className={cn("space-y-2", className)}>
      <p className="text-sm font-medium">Modèle de document</p>
      <div className="grid gap-2 sm:grid-cols-2 lg:grid-cols-4">
        {TEMPLATES.map((t) => {
          const selected = value === t.id;
          return (
            <button
              key={t.id}
              type="button"
              onClick={() => onChange(t.id)}
              className={cn(
                "rounded-xl border px-3 py-3 text-left transition",
                selected
                  ? "border-[var(--brand)] bg-[var(--brand)]/5 ring-1 ring-[var(--brand)]/30"
                  : "border-border hover:bg-muted/50"
              )}
            >
              <span className="flex items-center gap-2">
                <span
                  className="size-3 rounded-full"
                  style={{ background: t.accent }}
                  aria-hidden
                />
                <span className="font-medium">{t.nom}</span>
              </span>
              <span className="mt-1 block text-xs text-muted-foreground">
                {t.description}
              </span>
            </button>
          );
        })}
      </div>
    </div>
  );
}
