"use client";

import { Lightbulb, Lock } from "lucide-react";
import { cn } from "@/shared/lib/utils";
import { useT } from "@/shared/i18n/client";
import { ANNOUNCEMENT_FORMATS, FORMAT_CLASSES } from "@/shared/constants/announcements";
import { applyFormatChange, newStep } from "@/shared/lib/announcements";
import { FormatIcon } from "@/presentation/components/announcements/announcement-badges";
import { EditorSection, type SectionProps } from "@/presentation/components/announcements/editor-sections/section";
import type { AnnouncementFormat } from "@/domain/entities/announcement";

/** The six formats as cards; "Info-bulle sur un élément" is the one-step tour shortcut. */
export function FormatSection({ input, update, locked, disabled }: SectionProps) {
  const t = useT();
  const a = t.announcements;
  const choose = (format: AnnouncementFormat, singleStep = false) => {
    const next = applyFormatChange(input, format);
    if (singleStep) next.steps = next.steps.slice(0, 1);
    // A full tour has at least two steps, otherwise it is the one-step tooltip.
    else if (format === "tour" && next.steps.length < 2) next.steps = [...next.steps, newStep()];
    update(next);
  };

  const cards: { format: AnnouncementFormat; singleStep?: boolean; name: string; use: string }[] = [
    ...ANNOUNCEMENT_FORMATS.map((f) => ({ format: f, name: a.formats[f].name, use: a.formats[f].use })),
    { format: "tour", singleStep: true, name: a.singleStepTour, use: a.singleStepTourHelp },
  ];

  const isActive = (card: (typeof cards)[number]) =>
    card.format === input.format && (card.format !== "tour" || !!card.singleStep === (input.steps.length === 1));

  return (
    <EditorSection id="section-format" index={1} title={a.editor.sections.format.title} help={a.editor.sections.format.help}>
      {locked && (
        <p className="flex items-center gap-1.5 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-xs text-amber-800">
          <Lock className="h-3.5 w-3.5" />
          {a.editor.formatLocked}
        </p>
      )}
      <div className="grid gap-2 sm:grid-cols-2 xl:grid-cols-3" role="radiogroup" aria-label={a.editor.sections.format.title}>
        {cards.map((card) => {
          const active = isActive(card);
          const blocked = disabled || (locked && !active);
          return (
            <button
              key={`${card.format}-${card.singleStep ? 1 : 0}`}
              type="button"
              role="radio"
              aria-checked={active}
              disabled={blocked}
              onClick={() => choose(card.format, card.singleStep)}
              className={cn(
                "flex items-start gap-3 rounded-lg border p-3 text-left transition-colors disabled:cursor-not-allowed disabled:opacity-50",
                active ? "border-blue-600 bg-blue-50/60 ring-1 ring-blue-600" : "hover:border-blue-300 hover:bg-muted/40",
              )}
            >
              <span className={cn("flex h-8 w-8 shrink-0 items-center justify-center rounded-md border", FORMAT_CLASSES[card.format])}>
                <FormatIcon format={card.format} className="h-4 w-4" />
              </span>
              <span className="min-w-0">
                <span className="block text-sm font-semibold">{card.name}</span>
                <span className="block text-xs leading-snug text-muted-foreground">{card.use}</span>
              </span>
            </button>
          );
        })}
      </div>
      <details className="rounded-lg border bg-muted/30 px-3 py-2 text-xs">
        <summary className="flex cursor-pointer items-center gap-1.5 font-medium">
          <Lightbulb className="h-3.5 w-3.5 text-amber-500" />
          {a.practicesTitle}
        </summary>
        <ul className="mt-2 list-disc space-y-1 pl-5 text-muted-foreground">
          {a.practices.map((p) => (
            <li key={p}>{p}</li>
          ))}
        </ul>
      </details>
    </EditorSection>
  );
}
