import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { requireAdmin, unauthorized } from "@/lib/admin-api";

export async function GET() {
  const admin = await requireAdmin();
  if (!admin) return unauthorized();
  return NextResponse.json(
    await prisma.siteText.findMany({ orderBy: { key: "asc" } })
  );
}

export async function POST(req: Request) {
  const admin = await requireAdmin();
  if (!admin) return unauthorized();
  const body = await req.json();
  const t = await prisma.siteText.upsert({
    where: { key: body.key },
    create: { key: body.key, value: body.value ?? "" },
    update: { value: body.value ?? "" },
  });
  return NextResponse.json(t);
}
