/**
 * GET /api/ai-strategist/sessions/[id]/messages — get all messages
 */
import { NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { prisma } from "@/lib/prisma";

export const dynamic = "force-dynamic";

export async function GET(_req: Request, { params }: { params: { id: string } }) {
  const user = await getCurrentUser();
  if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const session = await prisma.strategistSession.findUnique({ where: { id: params.id }, select: { userId: true } });
  if (!session || session.userId !== user.id) return NextResponse.json({ error: "Not found" }, { status: 404 });

  const messages = await prisma.strategistMessage.findMany({
    where: { sessionId: params.id },
    orderBy: { createdAt: "asc" },
    select: { id: true, role: true, content: true, artifacts: true, createdAt: true },
  });

  return NextResponse.json(messages);
}
