import { getSessionUser } from "@/lib/auth";
import { refreshMailboxFileDrawerIndexByEmail } from "@/lib/official-mail";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";

export async function POST() {
  const user = await getSessionUser();
  if (!user) return Response.json({ error: "unauthorized" }, { status: 401 });

  try {
    const result = await refreshMailboxFileDrawerIndexByEmail(user.email);
    return Response.json({ ok: true, ...result });
  } catch {
    return Response.json({ error: "file-index-sync-failed" }, { status: 503 });
  }
}
