// admin/otp.js
// Copyright (C) 2021 Digital Telepresence, LLC
// License: Apache-2.0

'use strict';

const express = require('express');
// const multer = require('multer');

const { SiteController, SiteError } = require('../../../lib/site-lib');

class OtpAdminController extends SiteController {

  constructor (dtp) {
    super(dtp, module.exports);
  }

  async start ( ) {
    // const upload = multer({ dest: `/tmp/${this.dtp.config.site.domainKey}/uploads/${module.exports.slug}` });

    const router = express.Router();
    router.use(async (req, res, next) => {
      res.locals.currentView = 'admin';
      res.locals.adminView = 'otp';
      return next();
    });

    // router.param('otp', this.populateOtp.bind(this));
    
    router.get('/', this.getIndex.bind(this));

    return router;
  }

  async getIndex (req, res, next) {
    try {
      const { otpAuth: otpAuthService } = this.dtp.services;
      if (!req.user) {
        throw new SiteError(402, "Error getting user");
      }
      res.locals.tokens = await otpAuthService.getBackupTokens(req.user, "Admin");
      res.render('admin/otp/index');
    } catch (error) {
      this.log.error('failed to get tokens', { error });
      return next(error);
    }
  }

}

module.exports = {
  name: 'adminOtp',
  slug: 'admin-opt',
  create: async (dtp) => { return new OtpAdminController(dtp); },
};