import { attachOperationMetadataHeader } from "../../../lib/operationMetadata.js";
import { resolveMessageContext } from "../../../lib/routeHelpers.js";
import { resolveUserFeatures, runMessageMutationGuardAfterSuccess, runMessageMutationGuards } from "../../guards.js";
import {
  conversationMutationResponseSchema,
  errorResponseSchema
} from "../../openapi.js";
const metadata = {
  DELETE: { requireAuth: true, requireFeatures: ["messages.view"] }
};
async function DELETE(req, { params }) {
  const { ctx, scope } = await resolveMessageContext(req);
  const commandBus = ctx.container.resolve("commandBus");
  const guardResult = await runMessageMutationGuards(
    ctx.container,
    {
      tenantId: scope.tenantId,
      organizationId: scope.organizationId,
      userId: scope.userId,
      resourceKind: "messages.conversation",
      resourceId: params.id,
      operation: "delete",
      requestMethod: req.method,
      requestHeaders: req.headers,
      mutationPayload: null
    },
    resolveUserFeatures(ctx.auth)
  );
  if (!guardResult.ok) {
    return Response.json(
      guardResult.errorBody ?? { error: "Operation blocked by guard" },
      { status: guardResult.errorStatus ?? 422 }
    );
  }
  try {
    const { result, logEntry } = await commandBus.execute("messages.conversation.delete_for_actor", {
      input: {
        anchorMessageId: params.id,
        tenantId: scope.tenantId,
        organizationId: scope.organizationId,
        userId: scope.userId
      },
      ctx: {
        container: ctx.container,
        auth: ctx.auth ?? null,
        organizationScope: null,
        selectedOrganizationId: scope.organizationId,
        organizationIds: scope.organizationId ? [scope.organizationId] : null,
        request: req
      }
    });
    const response = Response.json(result);
    attachOperationMetadataHeader(response, logEntry, {
      resourceKind: "messages.conversation",
      resourceId: params.id
    });
    await runMessageMutationGuardAfterSuccess(guardResult.afterSuccessCallbacks, {
      tenantId: scope.tenantId,
      organizationId: scope.organizationId,
      userId: scope.userId,
      resourceKind: "messages.conversation",
      resourceId: params.id,
      operation: "delete",
      requestMethod: req.method,
      requestHeaders: req.headers
    });
    return response;
  } catch (error) {
    if (error instanceof Error) {
      if (error.message === "Message not found") {
        return Response.json({ error: error.message }, { status: 404 });
      }
      if (error.message === "Access denied") {
        return Response.json({ error: error.message }, { status: 403 });
      }
    }
    throw error;
  }
}
const openApi = {
  tag: "Messages",
  methods: {
    DELETE: {
      summary: "Delete conversation for current actor",
      responses: [
        { status: 200, description: "Conversation deleted", schema: conversationMutationResponseSchema }
      ],
      errors: [
        { status: 403, description: "Access denied", schema: errorResponseSchema },
        { status: 404, description: "Message not found", schema: errorResponseSchema }
      ]
    }
  }
};
export {
  DELETE,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
