import { NextResponse } from "next/server";
import { z } from "zod";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import {
  COMMUNICATION_CHANNELS_TOGGLE_OUTBOUND_REACTION_COMMAND_ID
} from "../../../../../../commands/toggle-outbound-reaction.js";
import { MessageReaction } from "../../../../../../data/entities.js";
import { validateRouteMutationGuard } from "../../../../../../lib/route-mutation-guard.js";
const metadata = {
  path: "/communication_channels/messages/[messageId]/reactions/[reactionId]",
  DELETE: {
    requireAuth: true,
    requireFeatures: ["communication_channels.react"]
  }
};
async function DELETE(_req, context) {
  const { messageId, reactionId } = await context.params;
  if (!messageId || !reactionId || !z.string().uuid().safeParse(messageId).success || !z.string().uuid().safeParse(reactionId).success) {
    return NextResponse.json({ error: "Invalid params" }, { status: 400 });
  }
  const auth = context.auth;
  if (!auth?.sub || !auth?.tenantId) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  const container = await createRequestContainer();
  const em = container.resolve("em").fork();
  const reaction = await findOneWithDecryption(
    em,
    MessageReaction,
    {
      id: reactionId,
      messageId,
      tenantId: auth.tenantId,
      organizationId: auth.orgId ?? null
    },
    void 0,
    { tenantId: auth.tenantId, organizationId: auth.orgId ?? null }
  );
  if (!reaction) {
    return NextResponse.json({ error: "Reaction not found" }, { status: 404 });
  }
  const guard = await validateRouteMutationGuard({
    container,
    req: _req,
    auth,
    input: {
      resourceKind: "communication_channels.message",
      resourceId: messageId,
      operation: "custom",
      mutationPayload: { reactionId, emoji: reaction.emoji, action: "remove" }
    }
  });
  if ("response" in guard) return guard.response;
  const commandBus = container.resolve("commandBus");
  const input = {
    messageId,
    emoji: reaction.emoji,
    action: "remove",
    reactionId,
    reactedByUserId: auth.sub,
    scope: {
      tenantId: auth.tenantId,
      organizationId: auth.orgId ?? null
    }
  };
  const { result } = await commandBus.execute(COMMUNICATION_CHANNELS_TOGGLE_OUTBOUND_REACTION_COMMAND_ID, {
    input,
    ctx: {
      container,
      auth,
      organizationScope: null,
      selectedOrganizationId: auth.orgId ?? null,
      organizationIds: auth.orgId ? [auth.orgId] : null
    }
  });
  if (result.status === "noop") {
    return NextResponse.json({ error: result.reason }, { status: 404 });
  }
  if (result.status === "not_owner") {
    return NextResponse.json({ error: result.reason }, { status: 403 });
  }
  if (result.status === "no_channel_link") {
    return NextResponse.json({ error: result.reason }, { status: 409 });
  }
  await guard.afterSuccess();
  return new NextResponse(null, { status: 204 });
}
const openApi = {
  tags: ["CommunicationChannels"],
  methods: {
    DELETE: {
      summary: "Remove a reaction from a channel-linked message",
      tags: ["CommunicationChannels"],
      responses: [
        { status: 204, description: "Reaction removed" },
        { status: 400, description: "Invalid params" },
        { status: 401, description: "Unauthorized" },
        { status: 403, description: "Channel is owned by another user" },
        { status: 404, description: "Reaction not found or not owned by current user" },
        { status: 409, description: "Message not channel-linked" }
      ]
    }
  }
};
var route_default = DELETE;
export {
  DELETE,
  route_default as default,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
