import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getSessionUser } from "@/lib/auth/human-auth";
import { createHumanCommentSchema } from "@/lib/validators/comment";
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const projectId = searchParams.get("projectId");
if (!projectId) {
return NextResponse.json(
{ error: "projectId query parameter is required" },
{ status: 400 }
);
}
const comments = await prisma.humanComment.findMany({
where: { projectId, parentId: null },
include: {
user: { select: { id: true, displayName: true } },
replies: {
include: {
user: { select: { id: true, displayName: true } },
},
orderBy: { createdAt: "asc" },
},
},
orderBy: { createdAt: "desc" },
});
return NextResponse.json({ comments });
} catch (error) {
console.error("List human comments error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
const user = await getSessionUser(request);
if (!user) {
return NextResponse.json(
{ error: "Not authenticated" },
{ status: 401 }
);
}
const body = await request.json();
const parsed = createHumanCommentSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ error: "Validation failed", details: parsed.error.flatten() },
{ status: 400 }
);
}
const { content, projectId, parentId } = parsed.data;
// Verify project exists
const project = await prisma.project.findUnique({
where: { id: projectId },
});
if (!project) {
return NextResponse.json(
{ error: "Project not found" },
{ status: 404 }
);
}
// Verify parent comment if provided
if (parentId) {
const parent = await prisma.humanComment.findFirst({
where: { id: parentId, projectId },
});
if (!parent) {
return NextResponse.json(
{ error: "Parent comment not found" },
{ status: 404 }
);
}
}
const comment = await prisma.humanComment.create({
data: {
content,
userId: user.id,
projectId,
parentId: parentId || null,
},
include: {
user: { select: { id: true, displayName: true } },
},
});
return NextResponse.json({ comment }, { status: 201 });
} catch (error) {
console.error("Create human comment error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}