import { NextRequest, NextResponse } from 'next/server' import prisma from '@/lib/prisma' import { auth } from '@/auth' import { z } from 'zod' import { verifyParticipant } from '@/lib/brainstorm-collab' import { emitToSession } from '@/lib/socket-emit' const starSchema = z.object({ ideaId: z.string().min(1), }) export async function POST( request: NextRequest, { params }: { params: Promise<{ sessionId: string }> } ) { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } try { const { sessionId } = await params const body = await request.json() const { ideaId } = starSchema.parse(body) const brainstormSession = await prisma.brainstormSession.findFirst({ where: { id: sessionId }, }) if (!brainstormSession) { return NextResponse.json({ error: 'Session not found' }, { status: 404 }) } const { isParticipant } = await verifyParticipant(sessionId, session.user.id, 'editor') if (!isParticipant) { return NextResponse.json({ error: 'No edit permission' }, { status: 403 }) } const idea = await prisma.brainstormIdea.findFirst({ where: { id: ideaId, sessionId }, }) if (!idea) { return NextResponse.json({ error: 'Idea not found' }, { status: 404 }) } const updated = await prisma.brainstormIdea.update({ where: { id: ideaId }, data: { isStarred: !idea.isStarred }, }) await emitToSession(sessionId, 'idea:starred', { ideaId, isStarred: updated.isStarred, userId: session.user.id, }) return NextResponse.json({ success: true, data: { ideaId, isStarred: updated.isStarred } }) } catch (error) { console.error('Error starring idea:', error) return NextResponse.json({ error: 'Failed to star idea' }, { status: 500 }) } }