import { prisma } from "@/app/lib/prisma"
import { NextResponse } from "next/server"

export async function POST(
  request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  const { id } = await params
  const body = await request.json()
  const { role, content } = body

  if (!role || !content) {
    return NextResponse.json({ error: "role and content are required" }, { status: 400 })
  }

  const message = await prisma.message.create({
    data: {
      conversationId: parseInt(id),
      role,
      content,
    },
  })

  await prisma.conversation.update({
    where: { id: parseInt(id) },
    data: { updatedAt: new Date() },
  })

  return NextResponse.json(message)
}
