import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";
import { authOptions } from "@/lib/auth";
import { getThreadIdForReply } from "@/server/services/email-thread.service";

export default async function InboxEmailDetailPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const session = await getServerSession(authOptions);
  if (!session?.user) {
    redirect("/login?callbackUrl=/inbox");
  }

  const { id } = await params;
  const threadId = await getThreadIdForReply(id);
  if (threadId) {
    redirect(`/inbox?thread=${threadId}`);
  }
  redirect("/inbox");
}
