import { NextResponse } from "next/server";
import { z } from "zod";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { resolveOrganizationScopeForRequest } from "@open-mercato/core/modules/directory/utils/organizationScope";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { WorkflowInstance } from "../../../../data/entities.js";
import * as workflowExecutor from "../../../../lib/workflow-executor.js";
import { workflowInstanceResponseSchema, workflowExecutionResultSchema } from "../../../openapi.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("workflows");
const metadata = {
  requireAuth: true,
  requireFeatures: ["workflows.instances.retry"]
};
async function POST(request, context) {
  try {
    const params = await context.params;
    const container = await createRequestContainer();
    const em = container.resolve("em");
    const auth = await getAuthFromRequest(request);
    if (!auth) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
    const scope = await resolveOrganizationScopeForRequest({ container, auth, request });
    const tenantId = auth.tenantId;
    const organizationId = scope?.selectedId ?? auth.orgId;
    if (!tenantId || !organizationId) {
      return NextResponse.json(
        { error: "Missing tenant or organization context" },
        { status: 400 }
      );
    }
    const rbacService = container.resolve("rbacService");
    const hasPermission = await rbacService.userHasAllFeatures(
      auth.sub,
      ["workflows.instances.retry"],
      {
        tenantId,
        organizationId
      }
    );
    if (!hasPermission) {
      return NextResponse.json(
        { error: "Insufficient permissions" },
        { status: 403 }
      );
    }
    const instance = await findOneWithDecryption(em, WorkflowInstance, {
      id: params.id,
      tenantId,
      organizationId
    });
    if (!instance) {
      return NextResponse.json(
        { error: "Workflow instance not found" },
        { status: 404 }
      );
    }
    if (instance.status !== "FAILED") {
      return NextResponse.json(
        {
          error: `Cannot retry workflow in ${instance.status} status. Only FAILED workflows can be retried.`
        },
        { status: 400 }
      );
    }
    const previousErrorMessage = instance.errorMessage;
    const previousErrorDetails = instance.errorDetails;
    instance.status = "RUNNING";
    instance.retryCount = (instance.retryCount || 0) + 1;
    instance.errorMessage = null;
    instance.errorDetails = null;
    instance.updatedAt = /* @__PURE__ */ new Date();
    await em.flush();
    let result;
    try {
      result = await workflowExecutor.executeWorkflow(em, container, instance.id);
    } catch (executionError) {
      instance.status = "FAILED";
      instance.errorMessage = previousErrorMessage;
      instance.errorDetails = previousErrorDetails;
      instance.updatedAt = /* @__PURE__ */ new Date();
      await em.flush();
      throw executionError;
    }
    await em.refresh(instance);
    return NextResponse.json({
      data: {
        instance,
        execution: result
      },
      message: "Workflow retry initiated successfully"
    });
  } catch (error) {
    logger.error("Error retrying workflow instance", { err: error });
    if (error instanceof workflowExecutor.WorkflowExecutionError) {
      return NextResponse.json(
        { error: error.message },
        { status: 400 }
      );
    }
    return NextResponse.json(
      { error: "Failed to retry workflow instance" },
      { status: 500 }
    );
  }
}
const openApi = {
  methods: {
    POST: {
      summary: "Retry failed workflow instance",
      description: "Retry a failed workflow instance from its current step. The workflow will be reset to RUNNING status and execution will continue.",
      tags: ["Workflows"],
      params: z.object({
        id: z.string().uuid()
      }),
      responses: [
        {
          status: 200,
          description: "Workflow retry initiated successfully",
          schema: z.object({
            data: z.object({
              instance: workflowInstanceResponseSchema,
              execution: workflowExecutionResultSchema
            }),
            message: z.string()
          })
        },
        {
          status: 400,
          description: "Bad request - Workflow cannot be retried in current status or execution error",
          schema: z.object({ error: z.string() })
        },
        {
          status: 401,
          description: "Unauthorized",
          schema: z.object({ error: z.string() })
        },
        {
          status: 403,
          description: "Forbidden - Insufficient permissions",
          schema: z.object({ error: z.string() })
        },
        {
          status: 404,
          description: "Workflow instance not found",
          schema: z.object({ error: z.string() })
        },
        {
          status: 500,
          description: "Internal server error",
          schema: z.object({ error: z.string() })
        }
      ]
    }
  }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
