// admin/venue.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 VenueAdminController 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 = 'venue';
      return next();
    });

    router.param('channelId', this.populateChannelId.bind(this));
    router.param('channelSlug', this.populateChannelSlug.bind(this));

    router.post('/channel/:channelId', this.postUpdateChannel.bind(this));
    router.post('/channel', this.postCreateChannel.bind(this));

    router.get('/channel/create', this.getChannelEditor.bind(this));
    router.get('/channel/:channelSlug', this.getChannelEditor.bind(this));
    router.get('/channel', this.getChannelHome.bind(this));

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

    router.delete('/channel/:channelId', this.deleteChannel.bind(this));

    return router;
  }

  async populateChannelId (req, res, next, channelId) {
    const { venue: venueService } = this.dtp.services;
    try {
      res.locals.channel = await venueService.getChannelById(channelId, { withCredentials: true });
      return next();
    } catch (error) {
      this.log.error('failed to populate Venue channel', { channelId, error });
      return next(error);
    }
  }

  async populateChannelSlug (req, res, next, channelSlug) {
    const { venue: venueService } = this.dtp.services;
    try {
      res.locals.channel = await venueService.getChannelBySlug(channelSlug, { withCredentials: true });
      return next();
    } catch (error) {
      this.log.error('failed to populate Venue channel by slug', { channelSlug, error });
      return next(error);
    }
  }

  async postUpdateChannel (req, res, next) {
    const { user: userService, venue: venueService } = this.dtp.services;
    try {
      const owner = await userService.lookup(req.body.owner);
      if (!owner) {
        throw new SiteError(400, `Channel owner is empty or invalid: ${req.body.owner}`);
      }
      await venueService.updateChannel(res.locals.channel, owner, req.body);
      res.redirect('/admin/venue/channel');
    } catch (error) {
      this.log.error('failed to update Venue channel', { error });
      return next(error);
    }
  }

  async postCreateChannel (req, res, next) {
    const { user: userService, venue: venueService } = this.dtp.services;
    try {
      const owner = await userService.lookup(req.body.owner);
      if (!owner) {
        throw new SiteError(400, `Channel owner is empty or invalid: ${req.body.owner}`);
      }
      await venueService.createChannel(owner, req.body);
      res.redirect('/admin/venue/channel');
    } catch (error) {
      this.log.error('failed to create Venue channel', { error });
      return next(error);
    }
  }

  async getChannelEditor (req, res) {
    res.render('admin/venue/channel/editor');
  }

  async getChannelHome (req, res, next) {
    const { venue: venueService } = this.dtp.services;
    try {
      res.locals.pagination = this.getPaginationParameters(req, 20);
      res.locals.channels = await venueService.getChannels(res.locals.pagination);
      res.render('admin/venue/channel/index');
    } catch (error) {
      this.log.error('failed to present the Venue Admin home view', { error });
      return next(error);
    }
  }

  async getHomeView (req, res, next) {
    const { venue: venueService } = this.dtp.services;
    try {
      res.locals.channels = await venueService.getChannels();
      res.render('admin/venue/index');
    } catch (error) {
      this.log.error('failed to present the Venue Admin home view', { error });
      return next(error);
    }
  }

  async deleteChannel (req, res) {
    const { venue: venueService } = this.dtp.services;
    try {
      const displayList = this.createDisplayList('delete-channel');
      await venueService.removeChannel(res.locals.channel);
      displayList.navigateTo('/admin/venue');
      res.status(200).json({ success: true, displayList });
    } catch (error) {
      this.log.error('failed to delete channel', { error });
      res.status(error.statusCode || 500).json({
        success: false,
        message: error.message,
      });
    }
  }
}

module.exports = {
  name: 'adminVenue',
  slug: 'admin-venue',
  create: async (dtp) => { return new VenueAdminController(dtp); },
};