import { requireSession } from "@/lib/auth/guards";
import { searchBrigades } from "@/lib/search/query-brigades";
import { SearchTabs } from "@/components/search/SearchTabs";
import { SearchBar } from "@/components/search/SearchBar";
import { ResultList } from "@/components/search/results/ResultList";
import { BrigadeResultRow } from "@/components/search/results/BrigadeResultRow";
import { de } from "@/lib/i18n/de";

type SearchParamsRecord = Record<string, string | string[] | undefined>;

export default async function WehrenPage({
  searchParams,
}: {
  searchParams: Promise<SearchParamsRecord>;
}) {
  // Default-deny in der Tiefe (Querschnittsstandard 1).
  await requireSession();

  const sp = await searchParams;
  const qRaw = Array.isArray(sp.q) ? sp.q[0] : sp.q;
  const q = qRaw?.trim() || undefined;
  const hits = await searchBrigades(q);

  return (
    <div className="flex flex-col gap-4">
      <h1 className="text-xl font-semibold text-navy">{de.nav.wehren}</h1>
      <SearchTabs />
      <SearchBar placeholderKey="search.nameOrtPlz" />
      <ResultList count={hits.length}>
        {hits.map((hit) => (
          <BrigadeResultRow key={hit.brigadeId} hit={hit} />
        ))}
      </ResultList>
    </div>
  );
}
