import { Schema } from "mongoose";
import { applyIdTransform, registerModel, stringId } from "../schema";

const OtpChallengeSchema = new Schema(
  {
    _id: stringId,
    userId: { type: String, ref: "User", required: true, index: true },
    channel: { type: String, enum: ["EMAIL"], required: true },
    codeHash: { type: String, required: true },
    expiresAt: { type: Date, required: true },
    attempts: { type: Number, default: 0 },
    consumedAt: { type: Date, default: undefined },
  },
  { timestamps: { createdAt: true, updatedAt: false } },
);

OtpChallengeSchema.index({ userId: 1, channel: 1, consumedAt: 1 });
applyIdTransform(OtpChallengeSchema);

export const OtpChallenge = registerModel("OtpChallenge", OtpChallengeSchema);
