import DB from "../database";
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
import { ObjectID } from "bson";

export enum TFATypes {
   OTC,
   BACKUP_CODE,
   U2F,
   APP_ALLOW,
}

export const TFANames = new Map<TFATypes, string>();
TFANames.set(TFATypes.OTC, "Authenticator");
TFANames.set(TFATypes.BACKUP_CODE, "Backup Codes");
TFANames.set(TFATypes.U2F, "Security Key (U2F)");
TFANames.set(TFATypes.APP_ALLOW, "App Push");

export interface ITwoFactor extends ModelDataBase {
   user: ObjectID;
   valid: boolean;
   expires?: Date;
   name?: string;
   type: TFATypes;
   data: any;
}

export interface IOTC extends ITwoFactor {
   data: string;
}

export interface IYubiKey extends ITwoFactor {
   data: {
      registration?: any;
      publicKey: string;
      keyHandle: string;
   };
}

export interface IU2F extends ITwoFactor {
   data: {
      challenge?: string;
      publicKey: string;
      keyHandle: string;
      registration?: string;
   };
}

export interface IBackupCode extends ITwoFactor {
   data: string[];
}

const TwoFactor = DB.addModel<ITwoFactor>({
   name: "twofactor",
   versions: [
      {
         migration: (e) => {},
         schema: {
            user: { type: ObjectID },
            valid: { type: Boolean },
            expires: { type: Date, optional: true },
            name: { type: String, optional: true },
            type: { type: Number },
            data: { type: "any" },
         },
      },
   ],
});

export default TwoFactor;
