import { NextResponse } from "next/server";
import { z } from "zod";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { readJsonSafe } from "@open-mercato/shared/lib/http/readJsonSafe";
import { retrySyncSchema } from "../../../data/validators.js";
import { startDataSyncRun } from "../../../lib/start-run.js";
import {
  runCrudMutationGuardAfterSuccess,
  validateCrudMutationGuard
} from "@open-mercato/shared/lib/crud/mutation-guard";
const paramsSchema = z.object({ id: z.string().uuid() });
const metadata = {
  POST: { requireAuth: true, requireFeatures: ["data_sync.run"] }
};
const openApi = {
  tags: ["DataSync"],
  summary: "Retry a failed sync run"
};
async function POST(req, ctx) {
  const auth = await getAuthFromRequest(req);
  if (!auth?.tenantId || !auth.orgId) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  const rawParams = ctx.params && typeof ctx.params.then === "function" ? await ctx.params : ctx.params;
  const parsedParams = paramsSchema.safeParse(rawParams);
  if (!parsedParams.success) {
    return NextResponse.json({ error: "Invalid run id" }, { status: 400 });
  }
  const payload = await readJsonSafe(req);
  const parsedBody = retrySyncSchema.safeParse(payload ?? {});
  if (!parsedBody.success) {
    return NextResponse.json({ error: "Invalid payload", details: parsedBody.error.flatten() }, { status: 422 });
  }
  const container = await createRequestContainer();
  const syncRunService = container.resolve("dataSyncRunService");
  const progressService = container.resolve("progressService");
  const scope = { organizationId: auth.orgId, tenantId: auth.tenantId };
  const previous = await syncRunService.getRun(parsedParams.data.id, scope);
  if (!previous) {
    return NextResponse.json({ error: "Run not found" }, { status: 404 });
  }
  if (previous.status !== "failed" && previous.status !== "cancelled") {
    return NextResponse.json({ error: "Only failed or cancelled runs can be retried" }, { status: 409 });
  }
  const overlap = await syncRunService.findRunningOverlap(
    previous.integrationId,
    previous.entityType,
    previous.direction,
    scope
  );
  if (overlap) {
    return NextResponse.json({ error: "A sync run is already in progress for this integration and entity direction" }, { status: 409 });
  }
  const guardResult = await validateCrudMutationGuard(container, {
    tenantId: auth.tenantId,
    organizationId: scope.organizationId,
    userId: auth.sub,
    resourceKind: "data_sync.run",
    resourceId: previous.id,
    operation: "custom",
    requestMethod: req.method,
    requestHeaders: req.headers,
    mutationPayload: { action: "retry", ...parsedBody.data }
  });
  if (guardResult && !guardResult.ok) {
    return NextResponse.json(guardResult.body, { status: guardResult.status });
  }
  const cursor = parsedBody.data.fromBeginning ? null : previous.cursor ?? await syncRunService.resolveCursor(previous.integrationId, previous.entityType, previous.direction, scope);
  const { run, progressJob } = await startDataSyncRun({
    syncRunService,
    progressService,
    scope: {
      ...scope,
      userId: auth.sub
    },
    input: {
      integrationId: previous.integrationId,
      entityType: previous.entityType,
      direction: previous.direction,
      cursor,
      triggeredBy: auth.sub,
      batchSize: 100,
      progressJob: {
        name: `Retry data sync ${previous.integrationId} \u2014 ${previous.entityType}`
      }
    }
  });
  if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {
    await runCrudMutationGuardAfterSuccess(container, {
      tenantId: auth.tenantId,
      organizationId: scope.organizationId,
      userId: auth.sub,
      resourceKind: "data_sync.run",
      resourceId: run.id,
      operation: "custom",
      requestMethod: req.method,
      requestHeaders: req.headers,
      metadata: guardResult.metadata ?? null
    });
  }
  return NextResponse.json({ id: run.id, progressJobId: progressJob?.id ?? null }, { status: 201 });
}
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=retry.js.map
