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

'use strict';

const express = require('express');

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

class AdminImageController 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 = 'image';
      return next();
    });

    router.param('imageId', this.populateImageId.bind(this));

    router.get('/:imageId/archive-user', this.getUserArchiveView.bind(this));
    router.get('/:imageId', this.getImageView.bind(this));

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

    router.delete('/:imageId', this.deleteImage.bind(this));

    return router;
  }

  async populateImageId (req, res, next, imageId) {
    const {
      image: imageService,
      logan: loganService,
    } = this.dtp.services;
    try {
      res.locals.image = await imageService.getImageById(imageId);
      if (!res.locals.image) {
        throw new SiteError(404, 'Image not found');
      }
      return next();
    } catch (error) {
      loganService.sendRequestEvent(module.exports, req, {
        level: 'error',
        event: 'populateImageId',
        message: `failed to populate image: ${error.message}`,
        data: { imageId, error },
      });
      return next(error);
    }
  }

  async getUserArchiveView (req, res, next) {
    const { image: imageService } = this.dtp.services;
    try {
      res.locals.imageHistory = await imageService.getRecentImagesForOwner(res.locals.image.owner, 10);
      res.render('admin/image/archive-user');
    } catch (error) {
      return next(error);
    }
  }

  async getImageView (req, res) {
    res.render('admin/image/view');
  }

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

  async deleteImage (req, res) {
    const {
      image: imageService,
      logan: loganService,
    } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('delete-image');
      await imageService.deleteImage(res.locals.image);
      displayList.reload();

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

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

module.exports = {
  logId: 'ctl:admin:image',
  index: 'adminImage',
  className: 'AdminImageController',
  create: async (dtp) => { return new AdminImageController(dtp); },
};