import { logError } from "@/lib/logger";
import { NextRequest, NextResponse } from "next/server";
import { query } from "@/lib/db";
import { getAdminUserId } from "@/lib/auth";

export const runtime = "nodejs";

export async function DELETE(
  req: NextRequest,
  { params }: { params: Promise<{ id: string; msgId: string }> }
) {
  try {
    const adminUserId = getAdminUserId(req);
    if (!adminUserId) {
      return NextResponse.json({ error: "未登录" }, { status: 401 });
    }

    const { id, msgId } = await params;

    const result = await query<{ id: string }>(
      `UPDATE room_message
       SET revoked = NOW()
       WHERE id = $1 AND room = $2 AND revoked IS NULL
       RETURNING id`,
      [msgId, id]
    );

    if (!result.rows.length) {
      return NextResponse.json({ error: "消息不存在或已被撤回" }, { status: 404 });
    }

    return NextResponse.json({ success: true });
  } catch (e) {
    logError("Revoke message error:", e);
    return NextResponse.json({ error: "服务器错误" }, { status: 500 });
  }
}
