import { NextResponse } from "next/server";
import { isConvexEnabled } from "@/lib/convex/enabled";
import { getAuthedConvexClient } from "@/lib/convex/route";
import { api } from "@/lib/convex/api";

export const dynamic = "force-dynamic";

export async function POST(request: Request) {
  if (isConvexEnabled()) {
    const { client } = await getAuthedConvexClient();
    const payload = await request.json().catch(() => ({}));
    const workspaceId = payload.workspaceId as string | undefined;
    if (!workspaceId) {
      return NextResponse.json({ error: "缺少 workspaceId" }, { status: 400 });
    }

    await client.mutation(api.workspaces.switchDefaultWorkspace, {
      workspaceId,
    });

    return NextResponse.json({ success: true });
  }
  return NextResponse.json({ error: "仅支持 Convex 模式" }, { status: 501 });
}
