import { NextResponse } from "next/server";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
const routeMetadata = {
  GET: { requireAuth: true, requireFeatures: ["progress.view"] }
};
const metadata = routeMetadata;
async function GET(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth || !auth.tenantId) {
    return NextResponse.json({ active: [], recentlyCompleted: [] }, { status: 401 });
  }
  const container = await createRequestContainer();
  const progressService = container.resolve("progressService");
  const ctx = { tenantId: auth.tenantId, organizationId: auth.orgId };
  await progressService.markStaleJobsFailed(auth.tenantId, void 0, auth.orgId);
  const [jobs, recentlyCompleted] = await Promise.all([
    progressService.getActiveJobs(ctx),
    progressService.getRecentlyCompletedJobs(ctx)
  ]);
  return NextResponse.json({
    active: jobs.map(formatJob),
    recentlyCompleted: recentlyCompleted.map(formatJob)
  });
}
function formatJob(job) {
  return {
    id: job.id,
    jobType: job.jobType,
    name: job.name,
    description: job.description,
    status: job.status,
    progressPercent: job.progressPercent,
    processedCount: job.processedCount,
    totalCount: job.totalCount,
    etaSeconds: job.etaSeconds,
    cancellable: job.cancellable,
    meta: job.meta ?? null,
    startedAt: job.startedAt?.toISOString() ?? null,
    finishedAt: job.finishedAt?.toISOString() ?? null,
    errorMessage: job.errorMessage
  };
}
const openApi = {
  GET: {
    summary: "Get active progress jobs",
    description: "Returns currently running jobs and recently completed jobs for the progress top bar.",
    tags: ["Progress"],
    responses: {
      200: { description: "Active and recently completed jobs" }
    }
  }
};
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
