const mongoose = require('mongoose');

const referralSchema = new mongoose.Schema(
  {
    referrerId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Account',
      required: true,
    },
    referredId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Account',
      required: true,
    },
    bonusAmount: {
      type: Number,
      required: true,
      default: 500.0,
    },
    status: {
      type: String,
      enum: ['pending', 'completed', 'expired'],
      default: 'pending',
      required: true,
    },
    completedAt: {
      type: Date,
      required: false,
    },
  },
  {
    timestamps: true,
    collection: 'referrals',
  },
);

referralSchema.index({ referredId: 1 }, { unique: true });

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

module.exports = mongoose.model('Referral', referralSchema);
