import { Schema, model } from 'mongoose';
import { IOtpDoc } from '@/modules/otp/otp.interface.js';
import { toJSON } from '@/shared/utils/plugins/index.js';

const otpSchema = new Schema<IOtpDoc>({
  phone: {
    type: Number,
    required: true,
  },
  orderId: {
    type: String,
    required: true,
  },
  otp: {
    type: Number,
    required: true,
  },
  isVerified: {
    type: Boolean,
    default: false,
  },
  createdAt: {
    type: Date,
    default: Date.now,
    expires: 60 * 15, // ⏱ 900 seconds = 15 minutes
  },
});

// add plugin that converts mongoose to json
otpSchema.plugin(toJSON);

export const Otp = model<IOtpDoc>('Otp', otpSchema);
