import { NextRequest, NextResponse } from "next/server";
import prisma from "@/lib/db";
import { getSession } from "@/lib/auth";

export async function GET() {
    try {
        const data = await prisma.education.findMany({ orderBy: { startDate: "desc" } });
        return NextResponse.json(data);
    } catch { return NextResponse.json([], { status: 500 }); }
}

export async function POST(req: NextRequest) {
    const session = await getSession();
    if (!session) return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
    try {
        const body = await req.json();
        const data = await prisma.education.create({ data: body });
        return NextResponse.json(data);
    } catch { return NextResponse.json({ error: "Erreur" }, { status: 500 }); }
}
