export const dynamic = 'force-dynamic';

import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';

// POST /api/ideas
export async function POST(request: NextRequest) {
    const session = await auth();
    if (!session?.user?.id) {
        return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
    }

    const body = await request.json();

    const project = await getDB().project.findFirst({
        where: { id: body.projectId, userId: session.user.id },
    });
    if (!project) {
        return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
    }

    const idea = await getDB().idea.create({
        data: {
            title: body.title || 'Nouvelle idée',
            description: body.description || '',
            status: body.status || 'todo',
            category: body.category || 'plot',
            projectId: body.projectId,
        },
    });

    return NextResponse.json(idea, { status: 201 });
}
