import { NextResponse } from "next/server";
import bcrypt from "bcrypt";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient()


export async function POST(req: Request) {
    try {
        const body = await req.json();
        console.log("Données reçues:", body);

        const { email, firstName,lastName, city, phone, password, role, country, address } = body;
        const hashedPassword = await bcrypt.hash(password, 10);

        const newUser = await prisma.user.create({
            data: {
                email,
                firstName,
                lastName,
                phone,
                role,
                password: hashedPassword,
                city,
                country,
                address,
            },
        });

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

    } catch (error) {
        console.error("Registration error:", error);
        return NextResponse.json({ error: "Internal server error." }, { status: 500 });
    }
}
