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

export type SignupDraftDocument = HydratedDocument<SignupDraft>;

@Schema({ timestamps: true, versionKey: false })
export class SignupDraft {
  @Prop({ required: true, trim: true, lowercase: true, index: true })
  email!: string;

  @Prop({ required: true, select: false })
  passwordHash!: string;

  @Prop({ required: true, select: false })
  codeHash!: string;

  @Prop({ required: true })
  codeExpiresAt!: Date;

  @Prop({ type: Date, default: null })
  verifiedAt?: Date | null;

  @Prop({ type: String, default: '', select: false })
  signupTokenHash!: string;

  @Prop({ type: Date, default: null })
  signupTokenExpiresAt?: Date | null;

  @Prop({ default: 0, min: 0 })
  attempts!: number;

  @Prop({ default: false, index: true })
  consumed!: boolean;

  @Prop({ required: true })
  expiresAt!: Date;
}

export const SignupDraftSchema = SchemaFactory.createForClass(SignupDraft);

SignupDraftSchema.index({ email: 1, consumed: 1, expiresAt: -1 });
SignupDraftSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
