// src/models/email-verification.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved

import { Schema, model } from "mongoose";
import { GadgetId, IUser } from "@gadget/api";
import { nanoid } from "nanoid";

export enum EmailVerificationStatus {
  Pending = "pending",
  Verified = "verified",
  Expired = "expired",
}

export interface IEmailVerification {
  _id: GadgetId;
  createdAt: Date;
  user: IUser | GadgetId;
  code: string;
  status: EmailVerificationStatus;
}
export const EmailVerificationSchema = new Schema<IEmailVerification>({
  _id: { type: String, default: () => nanoid() },
  createdAt: { type: Date, required: true, default: Date.now },
  user: { type: String, required: true, index: 1, ref: "User" },
  code: { type: String, required: true, unique: true },
  status: {
    type: String,
    enum: EmailVerificationStatus,
    required: true,
    default: EmailVerificationStatus.Pending,
  },
});

export const EmailVerification = model<IEmailVerification>(
  "EmailVerification",
  EmailVerificationSchema,
);
export default EmailVerification;

(async () => {
  await EmailVerification.syncIndexes();
})();
