import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

export type OTPDocument = HydratedDocument<OTP>;

export enum OTPType {
    EMAIL = 'EMAIL',
    PHONE = 'PHONE'
}

@Schema()
export class OTP {
    @Prop({ type: String, default : null })
    email: string; // For email OTP

    @Prop({ type: String, default: null })
    mobile: string; // For phone OTP

    @Prop({ type: String, enum: OTPType, required: true })
    type: OTPType; // EMAIL or PHONE

    @Prop({ type: String, required: true })
    otp: string;

    @Prop({ type: Number, default: +new Date() })
    created_at: number;

    @Prop({ type: Number, default: +new Date() + 10 * 60 * 1000 }) // 10 minutes expiry
    expires_at: number;

    @Prop({ type: Boolean, default: false })
    is_used: boolean;
}

export const OTPSchema = SchemaFactory.createForClass(OTP);
