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

const SETTINGS_KEY = "mainpage_settings";

export async function GET() {
  const setting = await prisma.setting.findUnique({
    where: { key: SETTINGS_KEY },
  });
  const settings = setting ? JSON.parse(setting.value) : {};
  return NextResponse.json({ settings }, { headers: { "Cache-Control": "no-store" } });
}

export async function POST(req: Request) {
  const body = await req.json().catch(() => ({}));
  const value = JSON.stringify(body);
  const updated = await prisma.setting.upsert({
    where: { key: SETTINGS_KEY },
    update: { value },
    create: { key: SETTINGS_KEY, value },
  });
  const settings = JSON.parse(updated.value);
  return NextResponse.json({ settings, ok: true });
}

