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

export async function GET(_request: Request, context: { params: Promise<{ jobId: string }> }) {
  const user = await getSessionUser();
  if (!user) {
    return Response.json({ error: "unauthorized", ok: false }, { status: 401 });
  }

  try {
    const { jobId } = await context.params;
    const job = await getMailboxMessageMoveJobByEmail(user.email, jobId);
    return Response.json({ job, ok: true });
  } catch (error) {
    const message = error instanceof Error ? error.message : "job-not-found";
    return Response.json(
      { error: message === "job-not-found" ? message : "job-status-failed", ok: false },
      { status: message === "job-not-found" ? 404 : 500 }
    );
  }
}
