import { getSessionUser } from "@/lib/auth";
import { syncAllMailboxFoldersByEmail } 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", ok: false }, { status: 401 });
  }

  try {
    const result = await syncAllMailboxFoldersByEmail(user.email);
    return Response.json({ ok: true, result });
  } catch (error) {
    const message = error instanceof Error ? error.message : "full-sync-failed";
    console.error(`[mailbox-full-sync] failed mailbox=${user.email} message=${message}`);
    return Response.json({ error: "full-sync-failed", ok: false }, { status: 503 });
  }
}
