import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Types } from 'mongoose';
import { ReportReason } from '../../../common/enums/report-reason.enum';
import { User } from '../../users/schemas/user.schema';

export type ReportDocument = HydratedDocument<Report>;

export const REPORT_TARGET_TYPES = ['user', 'post', 'comment', 'listing', 'repair_shop'] as const;
export type ReportTargetType = (typeof REPORT_TARGET_TYPES)[number];

export const REPORT_STATUSES = ['open', 'in_review', 'resolved', 'rejected'] as const;
export type ReportStatus = (typeof REPORT_STATUSES)[number];

@Schema({ timestamps: true, versionKey: false })
export class Report {
  @Prop({ type: Types.ObjectId, ref: User.name, required: true, index: true })
  reporterId!: Types.ObjectId;

  @Prop({ type: String, enum: REPORT_TARGET_TYPES, required: true, index: true })
  targetType!: ReportTargetType;

  @Prop({ type: Types.ObjectId, required: true, index: true })
  targetId!: Types.ObjectId;

  @Prop({ type: String, enum: Object.values(ReportReason), required: true, index: true })
  reason!: ReportReason;

  @Prop({ default: '', trim: true, maxlength: 2000 })
  details!: string;

  @Prop({ type: String, enum: REPORT_STATUSES, default: 'open', index: true })
  status!: ReportStatus;

  @Prop({ default: '', trim: true, maxlength: 300 })
  resolutionNote!: string;

  @Prop({ type: String, default: '', index: true })
  resolvedBy!: string;

  @Prop({ type: Date, default: null })
  resolvedAt!: Date | null;
}

export const ReportSchema = SchemaFactory.createForClass(Report);
ReportSchema.index({ reporterId: 1, targetType: 1, targetId: 1 }, { unique: true });
ReportSchema.index({ status: 1, createdAt: -1 });
