import { NextResponse } from 'next/server' import prisma from '@/lib/prisma' import { generateSlug } from '@/lib/utils' export const dynamic = 'force-dynamic' export async function POST(req: Request) { try { const { promptId, creatorId } = await req.json() if (!promptId) { return NextResponse.json( { error: 'promptId is required' }, { status: 400 } ) } // Fetch the original prompt const original = await prisma.prompt.findUnique({ where: { id: promptId }, }) if (!original) { return NextResponse.json( { error: 'Prompt not found' }, { status: 404 } ) } // Generate new slug with "remix" suffix const newSlug = generateSlug(`${original.title} remix`) // Create the remix const remix = await prisma.prompt.create({ data: { slug: newSlug, title: `${original.title} (Remix)`, description: original.description, template: original.template, schema: original.schema ?? { variables: [] }, category: original.category, tags: original.tags, modelDefault: original.modelDefault, modelAllowed: original.modelAllowed, visibility: 'public', parentId: original.id, creatorId: creatorId || null, }, }) // Increment remix count on original await prisma.prompt.update({ where: { id: original.id }, data: { remixesCount: { increment: 1 } }, }) return NextResponse.json({ success: true, slug: remix.slug, id: remix.id, }) } catch (error) { console.error('Remix error:', error) return NextResponse.json( { error: 'Failed to create remix' }, { status: 500 } ) } }