import { NextResponse } from "next/server";
import { signOnlyOfficeConfig } from "@/lib/onlyoffice/rust-adapter";

export const dynamic = "force-dynamic";

export async function POST(request: Request) {
  try {
    const body = (await request.json().catch(() => null)) as { config?: Record<string, unknown> } | null;
    if (!body?.config) {
      return NextResponse.json({ error: "缺少 config" }, { status: 400 });
    }

    const result = await signOnlyOfficeConfig({
      request,
      config: body.config,
      secret: process.env.ONLYOFFICE_JWT_SECRET || "",
    });

    return NextResponse.json(result);
  } catch (error) {
    return NextResponse.json(
      { error: error instanceof Error ? error.message : "OnlyOffice 签名失败" },
      { status: 500 },
    );
  }
}
