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 { getIntegration } from "@open-mercato/shared/modules/integrations/types";
import { enforceCommandOptimisticLock } from "@open-mercato/shared/lib/crud/optimistic-lock-command";
import { isCrudHttpError } from "@open-mercato/shared/lib/crud/errors";
import { emitIntegrationsEvent } from "../../../events.js";
import { updateStateSchema } from "../../../data/validators.js";
import {
  resolveUserFeatures,
  runIntegrationMutationGuardAfterSuccess,
  runIntegrationMutationGuards
} from "../../guards.js";
const idParamsSchema = z.object({ id: z.string().min(1) });
const metadata = {
  PUT: { requireAuth: true, requireFeatures: ["integrations.manage"] }
};
const openApi = {
  tags: ["Integrations"],
  summary: "Update integration state"
};
async function PUT(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 = idParamsSchema.safeParse(rawParams);
  if (!parsedParams.success) {
    return NextResponse.json({ error: "Invalid integration id" }, { status: 400 });
  }
  const integration = getIntegration(parsedParams.data.id);
  if (!integration) {
    return NextResponse.json({ error: "Integration not found" }, { status: 404 });
  }
  const payload = await req.json().catch(() => null);
  const parsedBody = updateStateSchema.safeParse(payload);
  if (!parsedBody.success) {
    return NextResponse.json({ error: "Invalid state payload", details: parsedBody.error.flatten() }, { status: 422 });
  }
  const container = await createRequestContainer();
  const guardResult = await runIntegrationMutationGuards(
    container,
    {
      tenantId: auth.tenantId,
      organizationId: auth.orgId,
      userId: auth.sub ?? "",
      resourceKind: "integrations.integration",
      resourceId: integration.id,
      operation: "update",
      requestMethod: req.method,
      requestHeaders: req.headers,
      mutationPayload: parsedBody.data
    },
    resolveUserFeatures(auth)
  );
  if (!guardResult.ok) {
    return NextResponse.json(guardResult.errorBody ?? { error: "Operation blocked by guard" }, { status: guardResult.errorStatus ?? 422 });
  }
  let payloadData = parsedBody.data;
  if (guardResult.modifiedPayload) {
    const mergedPayload = { ...parsedBody.data, ...guardResult.modifiedPayload };
    const reparsed = updateStateSchema.safeParse(mergedPayload);
    if (!reparsed.success) {
      return NextResponse.json({ error: "Invalid state payload after guard transform", details: reparsed.error.flatten() }, { status: 422 });
    }
    payloadData = reparsed.data;
  }
  const stateService = container.resolve("integrationStateService");
  const stateScope = { organizationId: auth.orgId, tenantId: auth.tenantId };
  try {
    const current = await stateService.resolveState(integration.id, stateScope);
    enforceCommandOptimisticLock({
      resourceKind: "integrations.integration",
      resourceId: integration.id,
      current: current.updatedAt,
      request: req
    });
  } catch (error) {
    if (isCrudHttpError(error)) {
      return NextResponse.json(error.body, { status: error.status });
    }
    throw error;
  }
  const state = await stateService.upsert(
    integration.id,
    {
      isEnabled: payloadData.isEnabled,
      reauthRequired: payloadData.reauthRequired
    },
    stateScope
  );
  await emitIntegrationsEvent("integrations.state.updated", {
    integrationId: integration.id,
    isEnabled: state.isEnabled,
    reauthRequired: state.reauthRequired,
    tenantId: auth.tenantId,
    organizationId: auth.orgId,
    userId: auth.sub
  });
  await runIntegrationMutationGuardAfterSuccess(guardResult.afterSuccessCallbacks, {
    tenantId: auth.tenantId,
    organizationId: auth.orgId,
    userId: auth.sub ?? "",
    resourceKind: "integrations.integration",
    resourceId: integration.id,
    operation: "update",
    requestMethod: req.method,
    requestHeaders: req.headers
  });
  return NextResponse.json({
    isEnabled: state.isEnabled,
    reauthRequired: state.reauthRequired,
    apiVersion: state.apiVersion ?? null,
    updatedAt: state.updatedAt?.toISOString() ?? null
  });
}
export {
  PUT,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
