import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { Message, MessageRecipient } from "../../../data/entities.js";
import { buildForwardPreview } from "../../../lib/forwarding.js";
import { hasOrganizationAccess, resolveMessageContext } from "../../../lib/routeHelpers.js";
import {
  errorResponseSchema,
  forwardPreviewResponseSchema
} from "../../openapi.js";
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["messages.compose"] }
};
async function GET(req, { params }) {
  const { ctx, scope } = await resolveMessageContext(req);
  const em = ctx.container.resolve("em").fork();
  const message = await findOneWithDecryption(
    em,
    Message,
    {
      id: params.id,
      tenantId: scope.tenantId,
      deletedAt: null
    },
    void 0,
    { tenantId: scope.tenantId, organizationId: scope.organizationId }
  );
  if (!message) {
    return Response.json({ error: "Message not found" }, { status: 404 });
  }
  if (!hasOrganizationAccess(scope.organizationId, message.organizationId)) {
    return Response.json({ error: "Access denied" }, { status: 403 });
  }
  const recipient = await em.findOne(MessageRecipient, {
    messageId: params.id,
    recipientUserId: scope.userId,
    deletedAt: null
  });
  const isSender = message.senderUserId === scope.userId;
  const isRecipient = Boolean(recipient);
  if (!isSender && !isRecipient) {
    return Response.json({ error: "Access denied" }, { status: 403 });
  }
  try {
    const preview = await buildForwardPreview(em, {
      tenantId: scope.tenantId,
      organizationId: scope.organizationId,
      userId: scope.userId
    }, message);
    return Response.json(preview);
  } catch (error) {
    if (error instanceof Error && error.message === "Forward body exceeds maximum length") {
      return Response.json({ error: error.message }, { status: 413 });
    }
    throw error;
  }
}
const openApi = {
  tag: "Messages",
  methods: {
    GET: {
      summary: "Get forward preview for a message",
      responses: [
        { status: 200, description: "Forward preview generated", schema: forwardPreviewResponseSchema }
      ],
      errors: [
        { status: 403, description: "Access denied", schema: errorResponseSchema },
        { status: 404, description: "Message not found", schema: errorResponseSchema },
        { status: 413, description: "Forward body exceeds maximum length", schema: errorResponseSchema }
      ]
    }
  }
};
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
