import { NextResponse } from "next/server";
import { getMnoteRuntimeConfig } from "@/lib/runtime-config";
import { isConvexEnabled } from "@/lib/convex/enabled";

export async function POST(request: Request) {
  if (isConvexEnabled()) {
    // 说明：OCR 链路目前仍依赖 Supabase JWT/表结构，迁移阶段先显式禁用，避免 UI/接口误用。
    return NextResponse.json({ error: "Convex 模式暂不支持 OCR" }, { status: 501 });
  }

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

  /*
  const supabase = await createSupabaseRouteClient();
  const {
    data: { session },
  } = await supabase.auth.getSession();

  if (!session) {
    return NextResponse.json({ error: "未登录" }, { status: 401 });
  }

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

  const { error } = await supabase
    .from("media_assets")
    .update({ ocr_status: "processing" })
    .eq("id", assetId)
    .is("deleted_at", null)
    .limit(1);

  if (error) {
    return NextResponse.json({ error: error.message }, { status: 500 });
  }

  // 说明：同 /api/backend/health，优先走内网/本机后端，避免 Node fetch 被 FRP 自签证书拦截。
  const cfg = getMnoteRuntimeConfig();
  const backendUrl =
    process.env.BACKEND_INTERNAL_URL || process.env.BACKEND_URL || cfg.backendUrl;
  if (backendUrl) {
    void fetch(`${backendUrl}/api/v1/tasks/media-ocr`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ asset_id: assetId }),
    }).catch((err) => {
      console.warn("触发后端 OCR 失败", err);
    });
  }

  return NextResponse.json({ ok: true });
  */
}
