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, WorkflowEvent } from "../../../../data/entities.js";
import { workflowEventRowSchema, paginationSchema } 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 }
      );
    }
    const { searchParams } = new URL(request.url);
    const eventType = searchParams.get("eventType");
    const limit = parseInt(searchParams.get("limit") || "100");
    const offset = parseInt(searchParams.get("offset") || "0");
    const where = {
      workflowInstanceId: params.id,
      tenantId,
      ...orgFilter.where
    };
    if (eventType) {
      where.eventType = eventType;
    }
    const [events, total] = await em.findAndCount(
      WorkflowEvent,
      where,
      {
        orderBy: { occurredAt: "DESC" },
        limit,
        offset
      }
    );
    return NextResponse.json({
      data: events,
      pagination: {
        total,
        limit,
        offset,
        hasMore: offset + limit < total
      }
    });
  } catch (error) {
    logger.error("Error getting workflow events", { err: error });
    return NextResponse.json(
      { error: "Failed to get workflow events" },
      { status: 500 }
    );
  }
}
const openApi = {
  methods: {
    GET: {
      summary: "Get workflow instance events",
      description: "Get a chronological list of events for a workflow instance. Events track all state changes, transitions, and activities.",
      tags: ["Workflows"],
      params: z.object({
        id: z.string().uuid()
      }),
      query: z.object({
        eventType: z.string().optional(),
        limit: z.number().int().positive().default(100).optional(),
        offset: z.number().int().min(0).default(0).optional()
      }),
      responses: [
        {
          status: 200,
          description: "List of workflow events",
          schema: z.object({
            data: z.array(workflowEventRowSchema),
            pagination: paginationSchema
          })
        },
        {
          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
