import { y as defineEventHandler, b0 as useLogger, v as createError, aC as publicSettings, E as Event, U as User, aE as readBody, s as canModifyEvent, M as Message, as as notifyAdmins, K as enqueueMail } from '../../../../nitro/nitro.mjs';
import { g as getUser } from '../../../../_/getUser.mjs';
import 'luxon';
import '@sequelize/core';
import 'html-to-text';
import 'zod';
import 'crypto';
import 'node:fs';
import 'sharp';
import 'path';
import 'node:stream';
import 'jsdom';
import 'dompurify';
import 'mime-types';
import 'fs';
import 'fs/promises';
import 'linkify-html';
import '@peertube/http-signature';
import 'jsonld';
import 'lodash.intersection';
import 'email-templates';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'node:events';
import 'node:buffer';
import 'node:path';
import 'vue';
import 'vue-router';
import 'dotenv';
import 'url';
import 'bcryptjs';
import 'slugify';
import 'umzug';
import 'consola';
import '@sequelize/core/decorators-legacy';
import 'util';
import 'mitt';
import 'ical.js';
import 'node:url';
import 'fast-xml-parser';
import '../../../../_/jwt.mjs';
import 'jose';

const _event_id__post = defineEventHandler(async (evt) => {
  const log = useLogger("event:moderation");
  if (!publicSettings.enable_moderation) {
    log.error("Disabled");
    throw createError({ status: 403 });
  }
  const eventId = Number(evt.context.params?.event_id);
  const event = await Event.findByPk(eventId, { include: [{ model: User, required: false }] });
  if (!event) {
    log.warn(`Event does not exists: ${eventId}`);
    throw createError({ statusCode: 404 });
  }
  const body = await readBody(evt);
  const user = await getUser(evt);
  const isMine = user?.id === event.userId;
  const isAdminOrEditor = ["admin", "editor"].includes(user?.role ?? "");
  if (!canModifyEvent(event, user) && !publicSettings.enable_report) {
    log.warn(`Someone not allowed is trying to report an event -> "${event.title}" isMine: ${isMine} `);
    throw createError({ statusCode: 403 });
  }
  const author = isAdminOrEditor ? "ADMIN" : isMine ? "AUTHOR" : "ANON";
  try {
    const message = await Message.create({
      eventId,
      message: body.message,
      is_author_visible: body.is_author_visible || isMine,
      author
    });
    notifyAdmins("report", { event, message: body.message, author });
    log.info("Report event to admins");
    if (event?.user?.email && body.is_author_visible && !isMine) {
      enqueueMail(event.user.email, "report", { event, message: body.message, author });
    }
    return message;
  } catch (e) {
    log.warn(`[EVENT] ${e}`);
    throw createError({ statusCode: 500 });
  }
});

export { _event_id__post as default };
