import { Reveal } from '@/site/components/motion/reveal';
import { cx } from '@/site/lib/cx';
import type { QA } from '@/site/types';
import { Plus } from './icons';

type FaqListProps = {
    items: QA[];
    /** Unique name: opening one question closes the others in the group. */
    name: string;
    className?: string;
};

/**
 * Questions and answers as native <details>: works without JavaScript,
 * is keyboard accessible, opens on in-page search, and the answers stay in
 * the HTML for search engines. The open/close height animation is a
 * progressive enhancement (see `.faq-item` in site.css).
 *
 * The same items must be passed to Schemas::faqPage() on the server so the
 * FAQPage structured data matches what is visible (docs/SEO.md §5).
 */
export function FaqList({ items, name, className }: FaqListProps) {
    return (
        <div className={cx('border-t border-line', className)}>
            {items.map((item, index) => (
                <Reveal key={item.q} delay={Math.min(index, 5) * 50}>
                    <details
                        name={name}
                        className="faq-item group border-line border-b"
                    >
                        <summary className="hover:text-accent-fg flex cursor-pointer list-none items-start justify-between gap-6 py-6 text-lg font-medium transition-colors duration-300 md:py-7 md:text-xl [&::-webkit-details-marker]:hidden">
                            <span>{item.q}</span>
                            <span
                                aria-hidden="true"
                                className="border-line-strong ease-out-expo mt-1 grid size-8 shrink-0 place-items-center rounded-full border transition-transform duration-500 group-open:rotate-45"
                            >
                                <Plus className="size-4" />
                            </span>
                        </summary>
                        <div className="text-soft max-w-3xl pe-14 pb-7">
                            <p>{item.a}</p>
                        </div>
                    </details>
                </Reveal>
            ))}
        </div>
    );
}
