import { NextRequest, NextResponse } from "next/server";
import prisma from "@/lib/db";
import { getSession } from "@/lib/auth";

export async function GET() {
    try {
        const settings = await prisma.settings.findUnique({ where: { id: "global" } });
        return NextResponse.json(settings || {});
    } 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 data = await req.json();
        const settings = await prisma.settings.upsert({
            where: { id: "global" },
            update: data,
            create: { id: "global", ...data }
        });
        return NextResponse.json(settings);
    } catch { return NextResponse.json({ error: "Erreur" }, { status: 500 }); }
}
