import { NextRequest, NextResponse } from "next/server";
import prisma from "@/lib/db";
import { getSession } from "@/lib/auth";

export async function GET() {
    try {
        const profile = await prisma.profile.findFirst();
        return NextResponse.json(profile);
    } catch (e) { return NextResponse.json({ error: "Erreur serveur" }, { status: 500 }); }
}

export async function PUT(req: NextRequest) {
    const session = await getSession();
    if (!session) return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
    try {
        const body = await req.json();
        const { id, createdAt, updatedAt, ...data } = body;
        const profile = await prisma.profile.upsert({
            where: { id: "default" },
            update: data,
            create: { id: "default", ...data }
        });
        return NextResponse.json(profile);
    } catch (e) {
        console.error("Profile Update Error:", e);
        return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
    }
}
