import mongoose, { Schema, Document, Model } from 'mongoose';

export interface ILoginAttempt extends Document {
  ip: string;
  email: string;
  createdAt: Date;
}

const LoginAttemptSchema = new Schema<ILoginAttempt>({
  ip: { type: String, required: true, index: true },
  email: { type: String, required: true, index: true },
  createdAt: { type: Date, default: Date.now, expires: 900 } // TTL index: auto delete after 15 minutes (900 seconds)
});

// Compound indexes for fast counts
LoginAttemptSchema.index({ ip: 1, email: 1 });

export const LoginAttempt: Model<ILoginAttempt> =
  mongoose.models.LoginAttempt || mongoose.model<ILoginAttempt>('LoginAttempt', LoginAttemptSchema);
export default LoginAttempt;
