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()) {
    return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
  }

  const { tableId } = (await request.json().catch(() => ({}))) as { tableId?: string };
  if (!tableId) {
    return NextResponse.json({ error: "缺少 tableId" }, { status: 400 });
  }

  const { auth, client } = await getAuthedConvexClient();
  try {
    await client.mutation(api.tables.restore, { userId: auth.userId, tableId });
    return NextResponse.json({ success: true }, { status: 200 });
  } catch (error) {
    console.error("Convex API error:", error);
    return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
  }
}

