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

'use strict';

const express = require('express');
const mongoose = require('mongoose');

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

class UserController extends SiteController {

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

  async start ( ) {
    const { dtp } = this;
    const {
      limiter: limiterService,
      otpAuth: otpAuthService,
      session: sessionService,
    } = dtp.services;

    const upload = this.createMulter();

    const router = express.Router();
    dtp.app.use('/user', router);

    const authRequired = sessionService.authCheckMiddleware({ requireLogin: true });

    const otpSetup = otpAuthService.middleware('Account', {
      adminRequired: false,
      otpRequired: true,
      otpRedirectURL: async (req) => { return `/user/${req.user.username}`; },
    });
    const otpMiddleware = otpAuthService.middleware('Account', {
      adminRequired: false,
      otpRequired: false,
      otpRedirectURL: async (req) => { return `/user/${req.user.username}`; },
    });

    router.use(
      async (req, res, next) => {
        try {
          res.locals.currentView = 'user';
          res.locals.pageTitle = 'Manage your user account.';
          return next();
        } catch (error) {
          return next(error);
        }
      },
    );

    async function checkProfileOwner (req, res, next) {
      if (!req.user || !req.user._id.equals(res.locals.userProfile._id)) {
        return next(new SiteError(403, 'This is not your user account or profile'));
      }
      return next();
    }

    router.param('username', this.populateUsername.bind(this));
    router.param('userId', this.populateUserId.bind(this));
    router.param('coreUserId', this.populateCoreUserId.bind(this));

    router.post(
      '/core/:coreUserId/settings',
      limiterService.createMiddleware(limiterService.config.user.postUpdateCoreSettings),
      checkProfileOwner,
      upload.none(),
      this.postUpdateCoreSettings.bind(this),
    );

    router.post(
      '/:userId/profile-photo',
      limiterService.createMiddleware(limiterService.config.user.postProfilePhoto),
      checkProfileOwner,
      upload.single('imageFile'),
      this.postProfilePhoto.bind(this),
    );

    router.post(
      '/:userId/settings',
      limiterService.createMiddleware(limiterService.config.user.postUpdateSettings),
      checkProfileOwner,
      upload.none(),
      this.postUpdateSettings.bind(this),
    );

    router.post(
      '/',
      limiterService.createMiddleware(limiterService.config.user.postCreate),
      this.postCreateUser.bind(this),
    );

    router.get(
      '/core/:coreUserId/settings',
      limiterService.createMiddleware(limiterService.config.user.getSettings),
      authRequired,
      otpMiddleware,
      checkProfileOwner,
      this.getCoreUserSettingsView.bind(this),
    );
    router.get(
      '/core/:coreUserId',
      limiterService.createMiddleware(limiterService.config.user.getUserProfile),
      authRequired,
      otpMiddleware,
      this.getUserView.bind(this),
    );

    router.get(
      '/:userId/otp-setup',
      limiterService.createMiddleware(limiterService.config.user.getOtpSetup),
      otpSetup,
      this.getOtpSetup.bind(this),
    );
    router.get(
      '/:userId/otp-disable',
      limiterService.createMiddleware(limiterService.config.user.getOtpDisable),
      authRequired,
      this.getOtpDisable.bind(this),
    );

    router.get(
      '/:userId/settings',
      limiterService.createMiddleware(limiterService.config.user.getSettings),
      authRequired,
      otpMiddleware,
      checkProfileOwner,
      this.getUserSettingsView.bind(this),
    );
    router.get(
      '/:username',
      limiterService.createMiddleware(limiterService.config.user.getUserProfile),
      authRequired,      
      otpMiddleware,
      this.getUserView.bind(this),
    );

    router.delete(
      '/:userId/profile-photo',
      limiterService.createMiddleware(limiterService.config.user.deleteProfilePhoto),
      authRequired,      
      checkProfileOwner,
      this.deleteProfilePhoto.bind(this),
    );
  }

  async populateUsername (req, res, next, username) {
    const { user: userService } = this.dtp.services;
    try {
      res.locals.userProfile = await userService.getPublicProfile('User', username);
      if (!res.locals.userProfile) {
        throw new SiteError(404, 'Member not found');
      }
      return next();
    } catch (error) {
      this.log.error('failed to populate username with public profile', { username, error });
      return next(error);
    }
  }

  async populateUserId (req, res, next, userId) {
    const { user: userService } = this.dtp.services;
    try {
      userId = mongoose.Types.ObjectId(userId);
    } catch (error) {
      return next(new SiteError(406, 'Invalid User'));
    }
    try {
      res.locals.userProfile = await userService.getUserAccount(userId);
      return next();
    } catch (error) {
      this.log.error('failed to populate userId', { userId, error });
      return next(error);
    }
  }

  async populateCoreUserId (req, res, next, coreUserId) {
    const { coreNode: coreNodeService } = this.dtp.services;
    try {
      coreUserId = mongoose.Types.ObjectId(coreUserId);
    } catch (error) {
      return next(new SiteError(406, 'Invalid User'));
    }
    try {
      res.locals.userProfile = await coreNodeService.getUserByLocalId(coreUserId);
      return next();
    } catch (error) {
      this.log.error('failed to populate coreUserId', { coreUserId, error });
      return next(error);
    }
  }

  async postCreateUser (req, res, next) {
    const { user: userService } = this.dtp.services;
    try {
      // verify that the request has submitted a captcha
      if ((typeof req.body.captcha !== 'string') || req.body.captcha.length === 0) {
        throw new SiteError(403, 'Invalid signup attempt');
      }
      // verify that the session has a signup captcha
      if (!req.session.captcha || !req.session.captcha.signup) {
        throw new SiteError(403, 'Invalid signup attempt');
      }
      // verify that the captcha from the form matches the captcha in the signup session flow
      if (req.body.captcha !== req.session.captcha.signup) {
        throw new SiteError(403, 'The captcha value is not correct');
      }

      // create the user account
      res.locals.user = await userService.create(req.body);

      // log the user in
      req.login(res.locals.user, (error) => {
        if (error) {
          return next(error);
        }
        res.redirect(`/user/${res.locals.user.username}`);
      });
    } catch (error) {
      this.log.error('failed to create new user', { error });
      return next(error);
    }
  }

  async postProfilePhoto (req, res) {
    const { user: userService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('profile-photo');
      await userService.updatePhoto(req.user, req.file);
      displayList.showNotification(
        'Profile photo updated successfully.',
        'success',
        'bottom-center',
        2000,
      );
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to update profile photo', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async postHeaderImage (req, res) {
    const { user: userService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('header-image');
      await userService.updateHeaderImage(req.user, req.file);
      displayList.showNotification(
        'Header image updated successfully.',
        'success',
        'bottom-center',
        2000,
      );
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to update header image', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async postUpdateCoreSettings (req, res) {
    const { coreNode: coreNodeService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('app-settings');

      await coreNodeService.updateUserSettings(req.user, req.body);

      displayList.reload();
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to update CoreUser settings', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async postUpdateSettings (req, res) {
    const { user: userService } = this.dtp.services;
    try {
      await userService.updateSettings(req.user, req.body);

      const displayList = this.createDisplayList('app-settings');
      displayList.reload();

      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to update account settings', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async getOtpSetup (req, res) {
    res.render('user/otp-setup-complete');
  }

  async getOtpDisable (req, res) {
    const { otpAuth: otpAuthService } = this.dtp.services;
    try {
      await otpAuthService.destroyOtpSession(req, 'Account');
      await otpAuthService.removeForUser(req.user, 'Account');
      res.render('user/otp-disabled');
    } catch (error) {
      this.log.error('failed to disable OTP service for Account', { error });
      res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async getCoreUserSettingsView (req, res, next) {
    const { otpAuth: otpAuthService } = this.dtp.services;
    try {
      res.locals.hasOtpAccount = await otpAuthService.isUserProtected(req.user, 'Account');
      res.locals.startTab = req.query.st || 'watch';
      res.render('user/settings-core');
    } catch (error) {
      this.log.error('failed to render CoreUser settings view', { error });
      return next(error);
    }
  }

  async getUserSettingsView (req, res, next) {
    const { otpAuth: otpAuthService } = this.dtp.services;
    try {
      res.locals.hasOtpAccount = await otpAuthService.isUserProtected(req.user, 'Account');
      res.locals.startTab = req.query.st || 'watch';
      res.render('user/settings');
    } catch (error) {
      this.log.error('failed to render user settings view', { error });
      return next(error);
    }
  }

  async getUserView (req, res, next) {
    const { comment: commentService } = this.dtp.services;
    try {
      res.locals.pageTitle = `${res.locals.userProfile.username}'s Profile`;
      res.locals.pagination = this.getPaginationParameters(req, 20);
      res.locals.commentHistory = await commentService.getForAuthor(req.user, res.locals.pagination);
      res.render('user/profile');
    } catch (error) {
      this.log.error('failed to produce user profile view', { error });
      return next(error);
    }
  }

  async deleteProfilePhoto (req, res) {
    const { user: userService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('app-settings');
      await userService.removePhoto(req.user);
      displayList.showNotification(
        'Profile photo removed successfully.',
        'success',
        'bottom-center',
        2000,
      );
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to remove profile photo', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }

  async deleteHeaderImage (req, res) {
    const { user: userService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('remove-header-image');
      await userService.removeHeaderImage(req.user);
      displayList.showNotification(
        'Header image removed successfully.',
        'success',
        'bottom-center',
        2000,
      );
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to remove header image', { error });
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }
}

module.exports = {
  slug: 'user',
  name: 'user',
  create: async (dtp) => { return new UserController(dtp); },
};
