"use client";

import { Card } from "@/components/ui/card";
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { ChevronDown, ChevronUp } from "lucide-react";

interface ExtraItem {
  title: string;
  description?: string | null;
}

interface ExtrasCardProps {
  extras: ExtraItem[];
  /** Which indices are open. Controlled by parent. */
  openIndices: Set<number>;
  onOpenChange: (index: number, open: boolean) => void;
  stripHtml: (html: string) => string;
  className?: string;
}

/**
 * Collapsible "Things to know" / extras cards.
 */
export function ExtrasCard({
  extras,
  openIndices,
  onOpenChange,
  stripHtml,
  className,
}: ExtrasCardProps) {
  if (!extras?.length) return null;

  return (
    <div className={`space-y-3 ${className ?? ""}`}>
      {extras.map((extra, idx) => (
        <Card
          key={idx}
          className="overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm"
        >
          <Collapsible
            open={openIndices.has(idx)}
            onOpenChange={(open) => onOpenChange(idx, open)}
          >
            <CollapsibleTrigger asChild>
              <button
                type="button"
                className="flex w-full items-center gap-2 px-4 py-3 text-left hover:bg-gray-50/80"
              >
                <span className="flex-1 font-semibold text-gray-900">
                  {extra.title}
                </span>
                {openIndices.has(idx) ? (
                  <ChevronUp className="h-4 w-4 shrink-0 text-gray-400" />
                ) : (
                  <ChevronDown className="h-4 w-4 shrink-0 text-gray-400" />
                )}
              </button>
            </CollapsibleTrigger>
            <CollapsibleContent>
              <div className="border-t border-gray-100 px-4 pb-4 pt-3">
                <p className="text-justify text-sm text-gray-700">
                  {extra.description ? stripHtml(extra.description) : "—"}
                </p>
              </div>
            </CollapsibleContent>
          </Collapsible>
        </Card>
      ))}
    </div>
  );
}
