export const dynamic = 'force-dynamic';

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

// POST /api/entities — Create an entity
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 entity = await getDB().entity.create({
        data: {
            type: body.type,
            name: body.name || 'Nouvelle entité',
            description: body.description || '',
            details: body.details || '',
            storyContext: body.storyContext || null,
            attributes: body.attributes || null,
            customValues: body.customValues || null,
            projectId: body.projectId,
        },
    });

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