import { prisma } from "@/lib/db";
import { timeAgo } from "@/lib/constants";
import ChatViewer from "./ChatViewer";

export const dynamic = "force-dynamic";

export default async function AdminChats() {
  const sessions = await prisma.chatSession.findMany({
    include: {
      project: true,
      messages: { orderBy: { id: "asc" } },
      _count: { select: { messages: true } },
    },
    orderBy: { id: "desc" },
    take: 50,
  });

  const withMessages = sessions.filter((s) => s._count.messages > 0);

  return (
    <main className="max-w-5xl mx-auto space-y-5">
      <div>
        <h1 className="text-2xl font-extrabold tracking-tight">AI chat conversations</h1>
        <p className="text-sm text-muted mt-1">
          What customers are asking the Gemini assistant — great for spotting missing help articles.
        </p>
      </div>
      <ChatViewer sessions={JSON.parse(JSON.stringify(withMessages))} />
    </main>
  );
}
