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

'use strict';

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

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

class ContentReportController extends SiteController {

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

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

    const authRequired = sessionService.authCheckMiddleware({ requiredLogin: true });
    const upload = multer({ dest: `/tmp/${this.dtp.config.site.domainKey}/uploads` });

    const router = express.Router();
    dtp.app.use('/content-report', router);

    router.use(async (req, res, next) => {
      res.locals.currentView = 'content-report';
      return next();
    });

    router.post('/comment/form',
      limiterService.createMiddleware(limiterService.config.contentReport.postCommentReportForm),
      authRequired,
      upload.none(),
      this.postCommentReportForm.bind(this),
    );
    router.post('/comment',
      limiterService.createMiddleware(limiterService.config.contentReport.postCommentReport),
      authRequired,
      upload.none(),
      this.postCommentReport.bind(this),
    );
  }

  async postCommentReportForm (req, res, next) {
    const { comment: commentService } = this.dtp.services;
    try {
      res.locals.comment = await commentService.getById(req.body.commentId);
      res.locals.params = req.body;
      res.render('comment/components/report-form');
    } catch (error) {
      return next(error);
    }
  }

  async postCommentReport (req, res) {
    const {
      contentReport: contentReportService,
      comment: commentService,
      user: userService,
    } = this.dtp.services;

    const displayList = this.createDisplayList('add-recipient');

    try {
      res.locals.report = await contentReportService.create(req.user, {
        resourceType: 'Comment',
        resourceId: req.body.commentId,
        category: req.body.category,
        reason: req.body.reason,
      });
      displayList.showNotification('Comment reported successfully', 'success', 'bottom-center', 5000);

      if (req.body.blockAuthor === 'on') {
        const comment = await commentService.getById(req.body.commentId);
        await userService.blockUser(req.user._id, comment.author._id || comment.author);
        displayList.showNotification('Comment author blocked successfully', 'success', 'bottom-center', 5000);
      }

      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to post comment report', { error });
      if (error.code === 11000) {
        displayList.showNotification(
          'You already reported this comment',
          'primary',
          'bottom-center',
          5000,
        );
        return res.status(200).json({ success: true, displayList });
      }
      return res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }
}

module.exports = {
  slug: 'content-report',
  name: 'contentReport',
  create: async (dtp) => {
    let controller = new ContentReportController(dtp);
    return controller;
  },
};