// otp-account.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

var OtpBackupTokenSchema = new Schema({
  token: { type: String, required: true },
  claimed: { type: Date },
});

const OtpAccountSchema = new Schema({
  created: { type: Date, default: Date.now, required: true, index: -1 },
  user: { type: Schema.ObjectId, required: true, index: true, ref: 'User' },
  service: { type: String, required: true },
  secret: { type: String, required: true, select: false },
  algorithm: { type: String, required: true },
  step: { type: Number, default: 30, required: true, min: 15 },
  digits: { type: Number, default: 6, required: true, min: 6 },
  backupTokens: { type: [OtpBackupTokenSchema], select: false },
  lastVerification: { type: Date },
  lastVerificationIp: { type: String },
});

OtpAccountSchema.index({
  user: 1,
  service: 1,
}, {
  unique: true,
  name: 'otp_user_svc_uniq_idx',
});

module.exports = mongoose.model('OtpAccount', OtpAccountSchema);