// Search-by-name page (T-508, REQ-SRCH-001).
//
// Server Component shell (same discipline as tree/page.tsx and the Trah
// dashboard): resolves the caller's own Membership for this trahId via
// getCallerMembership and redirects to /login if there isn't one —
// searchByName's own contract REQUIRES an ACTIVE viewerMembershipId (no
// public-view case, unlike getProfile), so gating the whole page behind
// login here is correct, not just a UX nicety.
//
// The actual search interaction (input + results list) is a small Client
// Component (search-panel.tsx) that fetches
// GET /api/trahs/[trahId]/search?q= as the user submits a query, rather
// than this Server Component calling searchByName() directly — unlike
// tree/page.tsx, this page has no server-known initial query to render on
// first paint, so there's no redundant-fetch tradeoff to avoid.
import { notFound, redirect } from "next/navigation";
import { prisma } from "@/lib/prisma";
import { getCallerMembership } from "@/lib/get-caller-membership";
import SearchPanel from "./search-panel";

export default async function SearchPage({
  params,
}: {
  params: Promise<{ trahId: string }>;
}) {
  const { trahId } = await params;

  const caller = await getCallerMembership(trahId);
  if (!caller) {
    redirect("/login");
  }

  const trah = await prisma.trah.findUnique({ where: { id: trahId } });
  if (!trah) {
    notFound();
  }

  return (
    <main style={{ maxWidth: 640, margin: "0 auto", padding: "2rem 1rem" }}>
      <h1>Search: {trah.name}</h1>
      <p style={{ fontSize: "0.85rem", color: "#666" }}>
        Search for a person by name within this Trah.
      </p>

      <SearchPanel trahId={trahId} />
    </main>
  );
}
