import { NextRequest, NextResponse } from "next/server";
import bcrypt from 'bcryptjs';
import prisma from "@/app/prisma";

interface Body {
    username: string;
    name: string;
    email: string;
    password: string;
}

export async function POST(request: NextRequest) {
    try {
        const body: Body = await request.json();

        if (!body.username || !body.name || !body.email || !body.password) {
            return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
        }

        const user = await prisma.user.create({
            data: {
                username: body.username,
                name: body.name,
                email: body.email,
                password: await bcrypt.hash(body.password, 10),
            },
        });

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

    } catch (error: any) {
        return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
    }
}
        