import mongoConnection from "$connections/mongo/mongo.connection.js";

import mongoose from "mongoose";

const mongooseSchema = mongoose.Schema;

export const schemaModel = {
  inviter: {
    type: mongooseSchema.Types.ObjectId,
    ref: "User",
    required: true,
    index: true,
  },
  invitee: {
    type: mongooseSchema.Types.ObjectId,
    ref: "User",
    required: true,
    unique: true,
    index: true,
  },
  referralCode: {
    type: String,
    required: true,
    trim: true,
    index: true,
  },
  status: {
    type: String,
    enum: ["pending", "qualified", "rejected"],
    default: "pending",
    index: true,
  },
  qualifiedAt: {
    type: Date,
    default: null,
  },
  meta: {
    type: mongooseSchema.Types.Mixed,
    default: {},
  },
};

export const schema = new mongooseSchema(schemaModel, { timestamps: true });

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

export default mongoConnection.model("ClubReferral", schema);
