import { PrismaClient } from "@/lib/generated/prisma/client";
import { NextResponse } from "next/server";

const prisma = new PrismaClient();

export async function GET() {
    try {
        const tests = await prisma.test.findMany();
        return NextResponse.json(tests);
    } catch (error) {
        console.error(error);
        return NextResponse.json({ error: "Failed to fetch tests." }, { status: 500 });
    }
}

export async function POST(req: Request) {
    try {
        const body = await req.json();
        const { name } = body;
        if (!name) {
            return NextResponse.json({ error: "Name is required." }, { status: 400 });
        }
        const test = await prisma.test.create({
            data: { name },
        });
        return NextResponse.json(test, { status: 201 });
    } catch (error) {
        return NextResponse.json({ error: "Failed to create test." }, { status: 500 });
    }
}
