// Relationship Finder page (T-509, REQ-SRCH-002): "how am I related to X?"
//
// Server Component shell (same discipline as tree/page.tsx and
// search/page.tsx): resolves the caller's own Membership for this trahId via
// getCallerMembership and redirects to /login if there isn't one —
// findRelationship's own contract REQUIRES an ACTIVE viewerMembershipId, so
// gating the whole page behind login here is correct, not just a UX nicety.
//
// The interactive picker + result display is a Client Component
// (relationship-finder-panel.tsx) that fetches
// GET /api/trahs/[trahId]/relationship-finder?a=&b= once both people are
// selected.
import { notFound, redirect } from "next/navigation";
import { prisma } from "@/lib/prisma";
import { getCallerMembership } from "@/lib/get-caller-membership";
import RelationshipFinderPanel from "./relationship-finder-panel";

export default async function RelationshipFinderPage({
  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>Relationship finder: {trah.name}</h1>
      <p style={{ fontSize: "0.85rem", color: "#666" }}>
        Pick two people to see how they're related and the chain connecting them.
      </p>

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