import { useState } from 'react';
import { DialogueArt } from '@/site/components/art/dialogue-art';
import { CtaBand } from '@/site/components/sections/cta-band';
import { PageHero, sheet } from '@/site/components/sections/page-hero';
import { FaqList } from '@/site/components/ui/faq-list';
import { Search } from '@/site/components/ui/icons';
import { useShared } from '@/site/hooks/use-shared';
import { useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';
import type { QA } from '@/site/types';

/**
 * General questions (Admin → SSS). The list is mirrored in FAQPage
 * structured data.
 *
 * A search box above the list narrows it to the questions that contain
 * every word typed (in the question or its answer). It only filters what
 * is already on the page: the full list is in the HTML, and the box stays
 * out of sight until the application runs (`.needs-js`), so nobody meets a
 * control that does nothing.
 */
export default function Faq({ faqs }: { faqs: QA[] }) {
    const { locale } = useShared();
    const t = useT();
    const [query, setQuery] = useState('');

    // Lower-cased by the page's own rules (Turkish: I → ı, İ → i).
    const fold = (text: string) => text.toLocaleLowerCase(locale);
    const words = fold(query).split(/\s+/).filter(Boolean);
    const shown =
        words.length === 0
            ? faqs
            : faqs.filter((item) => {
                  const text = fold(`${item.q} ${item.a}`);

                  return words.every((word) => text.includes(word));
              });

    return (
        <>
            <PageHero
                eyebrow={t.faq.eyebrow}
                title={t.faq.title}
                lead={t.faq.lead}
                art={<DialogueArt />}
            />

            <section className={cx('stage-light section-y', sheet)}>
                <div className="shell-narrow">
                    {faqs.length > 0 ? (
                        <>
                            <div role="search" className="needs-js mb-8">
                                <label htmlFor="faq-search" className="sr-only">
                                    {t.faq.search}
                                </label>
                                <div className="relative">
                                    <Search className="text-soft pointer-events-none absolute start-5 top-1/2 size-5 -translate-y-1/2" />
                                    <input
                                        id="faq-search"
                                        type="search"
                                        value={query}
                                        onChange={(event) =>
                                            setQuery(event.target.value)
                                        }
                                        placeholder={t.faq.search}
                                        autoComplete="off"
                                        className="border-line-strong bg-stage-2 placeholder:text-soft focus:border-accent-fg h-14 w-full rounded-full border ps-13 pe-6 text-lg transition-colors duration-300"
                                    />
                                </div>
                                {/* Says how many are left; always there, so the list does not jump. */}
                                <p
                                    aria-live="polite"
                                    className="text-soft mt-3 min-h-5 ps-5 text-sm"
                                >
                                    {words.length > 0
                                        ? t.faq.found(shown.length)
                                        : ''}
                                </p>
                            </div>

                            {shown.length > 0 ? (
                                <FaqList items={shown} name="faq" />
                            ) : (
                                <p className="text-lead text-soft">
                                    {t.faq.none}
                                </p>
                            )}
                        </>
                    ) : (
                        <p className="text-lead text-soft">{t.faq.empty}</p>
                    )}
                </div>
            </section>

            <CtaBand />
        </>
    );
}
