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

'use strict';

const express = require('express');

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

class AdminAttachmentController extends SiteController {

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

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

    router.param('attachmentId', this.populateAttachmentId.bind(this));

    router.post('/:attachmentId', this.postUpdateAttachment.bind(this));

    router.get('/:attachmentId', this.getAttachmentView.bind(this));

    router.get('/', this.getDashboard.bind(this));

    router.delete('/:attachmentId', this.deleteAttachment.bind(this));

    return router;
  }

  async populateAttachmentId (req, res, next, attachmentId) {
    const {
      attachment: attachmentService,
      logan: loganService,
    } = this.dtp.services;
    try {
      res.locals.attachment = await attachmentService.getById(attachmentId);
      return next();
    } catch (error) {
      loganService.sendRequestEvent(module.exports, req, {
        level: 'error',
        event: 'populateAttachmentId',
        message: `failed to populate attachment: ${error.message}`,
        data: { attachmentId, error },
      });
      return next(error);
    }
  }

  async postUpdateAttachment (req, res, next) {
    const {
      attachment: attachmentService,
      logan: loganService,
    } = this.dtp.services;
    try {
      await attachmentService.update(res.locals.attachment, req.body);
      loganService.sendRequestEvent(module.exports, req, {
        level: 'info',
        event: 'postUpdateAttachment',
        data: {
          attachment: {
            _id: res.locals.attachment._id,
          },
        },
      });
      res.redirect('/admin/attachment');
    } catch (error) {
      loganService.sendRequestEvent(module.exports, req, {
        level: 'error',
        event: 'postUpdateAttachment',
        message: `failed to update attachment: ${error.message}`,
        data: { error },
      });
      return next(error);
    }
  }

  async getAttachmentView (req, res) {
    res.render('admin/attachment/view');
  }

  async getDashboard (req, res, next) {
    const { attachment: attachmentService } = this.dtp.services;
    try {
      res.locals.pagination = this.getPaginationParameters(req, 20);
      res.locals.attachments = await attachmentService.getRecent(res.locals.pagination);
      res.render('admin/attachment/index');
    } catch (error) {
      return next(error);
    }
  }

  async deleteAttachment (req, res) {
    const {
      attachment: attachmentService,
      logan: loganService,
    } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('delete-attachment');
      await attachmentService.remove(res.locals.attachment);
      displayList.reload();

      res.status(200).json({ success: true, displayList });

      loganService.sendRequestEvent(module.exports, req, {
        level: 'info',
        event: 'deleteAttachment',
        data: { attachment: { _id: res.locals.attachment._id } },
      });
    } catch (error) {
      loganService.sendRequestEvent(module.exports, req, {
        level: 'error',
        event: 'deleteAttachment',
        message: `failed to delete attachment: ${error.message}`,
        data: { error },
      });
      res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }
}

module.exports = {
  logId: 'ctl:admin:attachment',
  index: 'adminAttachment',
  className: 'AdminAttachmentController',
  create: async (dtp) => { return new AdminAttachmentController(dtp); },
};