import { Message, MessageConfirmation, MessageRecipient } from "../../../data/entities.js";
import { resolveMessageContext } from "../../../lib/routeHelpers.js";
import {
  errorResponseSchema,
  messageConfirmationResponseSchema
} from "../../openapi.js";
const metadata = {
  GET: { requireAuth: true }
};
function hasOrganizationAccess(scopeOrganizationId, messageOrganizationId) {
  if (scopeOrganizationId) {
    return messageOrganizationId === scopeOrganizationId;
  }
  return messageOrganizationId == null;
}
async function GET(req, { params }) {
  const { ctx, scope } = await resolveMessageContext(req);
  const em = ctx.container.resolve("em").fork();
  const message = await em.findOne(Message, {
    id: params.id,
    tenantId: scope.tenantId,
    deletedAt: null
  });
  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: message.id,
    recipientUserId: scope.userId,
    deletedAt: null
  });
  const isSender = message.senderUserId === scope.userId;
  if (!isSender && !recipient) {
    return Response.json({ error: "Access denied" }, { status: 403 });
  }
  const confirmation = await em.findOne(MessageConfirmation, { messageId: message.id });
  return Response.json({
    messageId: message.id,
    confirmed: confirmation?.confirmed ?? false,
    confirmedAt: confirmation?.confirmedAt ? confirmation.confirmedAt.toISOString() : null,
    confirmedByUserId: confirmation?.confirmedByUserId ?? null
  });
}
const openApi = {
  tag: "Messages",
  methods: {
    GET: {
      summary: "Read message confirmation status",
      responses: [
        { status: 200, description: "Confirmation status", schema: messageConfirmationResponseSchema }
      ],
      errors: [
        { status: 403, description: "Access denied", schema: errorResponseSchema },
        { status: 404, description: "Message not found", schema: errorResponseSchema }
      ]
    }
  }
};
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
