import { NextResponse } from "next/server";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { listIntegrationLogsQuerySchema } from "@open-mercato/core/modules/integrations/data/validators";
import {
  finalizeIntegrationsReadResponse,
  integrationApiRoutePaths,
  runIntegrationsReadBeforeInterceptors
} from "../umes-read.js";
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["integrations.manage"] }
};
const openApi = {
  tags: ["Integrations"],
  summary: "List integration logs"
};
async function GET(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth?.tenantId || !auth.orgId) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  const container = await createRequestContainer();
  const beforeInterceptors = await runIntegrationsReadBeforeInterceptors({
    routePath: integrationApiRoutePaths.logs,
    request: req,
    auth,
    container
  });
  if (!beforeInterceptors.ok) {
    return NextResponse.json(beforeInterceptors.body, { status: beforeInterceptors.statusCode });
  }
  const query = beforeInterceptors.request.query ?? {};
  const parsed = listIntegrationLogsQuerySchema.safeParse({
    integrationId: typeof query.integrationId === "string" ? query.integrationId : void 0,
    level: typeof query.level === "string" ? query.level : void 0,
    runId: typeof query.runId === "string" ? query.runId : void 0,
    entityType: typeof query.entityType === "string" ? query.entityType : void 0,
    entityId: typeof query.entityId === "string" ? query.entityId : void 0,
    page: typeof query.page === "string" ? query.page : void 0,
    pageSize: typeof query.pageSize === "string" ? query.pageSize : void 0
  });
  if (!parsed.success) {
    return NextResponse.json({ error: "Invalid query", details: parsed.error.flatten() }, { status: 400 });
  }
  const logService = container.resolve("integrationLogService");
  const { items, total } = await logService.query(parsed.data, {
    organizationId: auth.orgId,
    tenantId: auth.tenantId
  });
  return finalizeIntegrationsReadResponse({
    routePath: integrationApiRoutePaths.logs,
    request: req,
    auth,
    container,
    interceptorRequest: beforeInterceptors.request,
    beforeMetadata: beforeInterceptors.metadataByInterceptor,
    enrich: {
      targetEntity: "integrations.log",
      listKeys: ["items"]
    },
    body: {
      items: items.map((item) => ({
        id: item.id,
        integrationId: item.integrationId,
        runId: item.runId ?? null,
        scopeEntityType: item.scopeEntityType ?? null,
        scopeEntityId: item.scopeEntityId ?? null,
        level: item.level,
        message: item.message,
        code: item.code ?? null,
        payload: item.payload ?? null,
        createdAt: item.createdAt.toISOString()
      })),
      total,
      page: parsed.data.page,
      pageSize: parsed.data.pageSize,
      totalPages: Math.max(1, Math.ceil(total / parsed.data.pageSize))
    }
  });
}
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
