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 { resolveOrganizationScopeFilter } from "@open-mercato/core/modules/directory/utils/organizationScopeFilter";
import { WorkflowInstance } from "../../../data/entities.js";
import { workflowInstanceResponseSchema } from "../../openapi.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("workflows");
const metadata = {
  requireAuth: true,
  requireFeatures: ["workflows.instances.view"]
};
async function GET(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 orgFilter = resolveOrganizationScopeFilter(scope, auth);
    if (!tenantId) {
      return NextResponse.json(
        { error: "Missing tenant context" },
        { status: 400 }
      );
    }
    const instance = await em.findOne(WorkflowInstance, {
      id: params.id,
      tenantId,
      ...orgFilter.where
    });
    if (!instance) {
      return NextResponse.json(
        { error: "Workflow instance not found" },
        { status: 404 }
      );
    }
    return NextResponse.json({ data: instance });
  } catch (error) {
    logger.error("Error getting workflow instance", { err: error });
    return NextResponse.json(
      { error: "Failed to get workflow instance" },
      { status: 500 }
    );
  }
}
const openApi = {
  methods: {
    GET: {
      summary: "Get workflow instance",
      description: "Get detailed information about a specific workflow instance including current state, context, and execution status.",
      tags: ["Workflows"],
      params: z.object({
        id: z.string().uuid()
      }),
      responses: [
        {
          status: 200,
          description: "Workflow instance details",
          schema: z.object({
            data: workflowInstanceResponseSchema
          })
        },
        {
          status: 401,
          description: "Unauthorized",
          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 {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
