import { Document, Model, Schema, Types, model } from "mongoose";

export type FeedbackStatus = "open" | "in_review" | "closed";

export interface IFeedback extends Document {
  userId?: Types.ObjectId;
  email?: string;
  name?: string;
  subject: string;
  message: string;
  status: FeedbackStatus;
  adminNotes: string;
}

const FeedbackSchema = new Schema<IFeedback>(
  {
    userId: { type: Schema.Types.ObjectId, ref: "User" },
    email: { type: String, trim: true },
    name: { type: String, trim: true },
    subject: { type: String, required: true, trim: true },
    message: { type: String, required: true },
    status: {
      type: String,
      enum: ["open", "in_review", "closed"],
      default: "open",
    },
    adminNotes: { type: String, default: "" },
  },
  { timestamps: true }
);

FeedbackSchema.index({ status: 1, createdAt: -1 });

const FeedbackModel: Model<IFeedback> = model<IFeedback>(
  "Feedback",
  FeedbackSchema
);

export default FeedbackModel;
