import { Types } from "mongoose";
import UserReportModel from "../models/UserReportModel";
import UserModel from "../models/UserModel";

export async function createReport(
  reporterId: string,
  reportedUserId: string,
  reason: string,
): Promise<Record<string, unknown>> {
  if (reporterId === reportedUserId) {
    throw new Error("CANNOT_SELF_REPORT");
  }

  const target = await UserModel.findOne({
    _id: new Types.ObjectId(reportedUserId),
    isDeleted: false,
  })
    .select("_id")
    .lean();

  if (!target) {
    throw new Error("USER_NOT_FOUND");
  }

  const report = await UserReportModel.create({
    reporterId: new Types.ObjectId(reporterId),
    reportedUserId: new Types.ObjectId(reportedUserId),
    reason: reason.trim(),
  });

  return {
    _id: report._id,
    reportedUserId: report.reportedUserId,
    reason: report.reason,
    createdAt: report.createdAt,
  };
}
