import express from 'express';
import _ from 'lodash';

import TemplateValidate from './validate/template';
import ConditionTemplateModel from '../models/condition_template';
import MeasurementTemplateModel from '../models/measurement_template';
import MaterialTemplateModel from '../models/material_template';
import res400 from './validate/res400';
import IdValidate from './validate/id';
import mongoose from "mongoose";
import db from '../db';



const router = express.Router();

router.get('/template/:collection(measurements|conditions|materials)', (req, res, next) => {
  if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'basic')) return;

  req.params.collection = req.params.collection.replace(/s$/g, '');  // remove trailing s
  model(req).find({}).lean().exec((err, data) => {
     if (err) next (err);
    // validate all and filter null values from validation errors
     res.json(_.compact(data.map(e => TemplateValidate.output(e))));
  });
});

router.get('/template/:collection(measurement|condition|material)/' + IdValidate.parameter(), (req, res, next) => {
  if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'basic')) return;

  model(req).findById(req.params.id).lean().exec((err, data) => {
    if (err) next (err);
    if (data) {
      res.json(TemplateValidate.output(data));
    }
    else {
      res.status(404).json({status: 'Not found'});
    }
  });
});

router.put('/template/:collection(measurement|condition|material)/' + IdValidate.parameter(),
  async (req, res, next) => {
  if (!req.auth(res, ['dev', 'admin'], 'basic')) return;

  const {error, value: template} = TemplateValidate.input(req.body, 'change');
  if (error) return res400(error, res);

  // find given template
  const templateRef = await model(req).findById(req.params.id).lean().exec().catch(err => {next(err);}) as any;
  if (templateRef instanceof Error) return;
  if (!templateRef) {
    return res.status(404).json({status: 'Not found'});
  }
  // find latest version
  const templateData = await model(req).findOne({first_id: templateRef.first_id}).sort({version: -1})
    .lean().exec().catch(err => {next(err);}) as any;
  if (templateData instanceof Error) return;
  if (!templateData) {
    return res.status(404).json({status: 'Not found'});
  }

  if (!_.isEqual(_.pick(templateData, _.keys(template)), template)) {  // data was changed
    template.version = templateData.version + 1;  // increase version
    // save new template, fill with old properties
    await new (model(req))(_.assign({}, _.omit(templateData, ['_id', '__v']), template)).save((err, data) => {
      if (err) next (err);
      db.log(req, req.params.collection + '_templates', {_id: data._id}, data.toObject());
      res.json(TemplateValidate.output(data.toObject()));
    });
  }
  else {
    res.json(TemplateValidate.output(templateData));
  }
});

router.post('/template/:collection(measurement|condition|material)/new', async (req, res, next) => {
  if (!req.auth(res, ['dev', 'admin'], 'basic')) return;

  const {error, value: template} = TemplateValidate.input(req.body, 'new');
  if (error) return res400(error, res);

  template._id = mongoose.Types.ObjectId();  // set reference to itself for first version of template
  template.first_id = template._id;
  template.version = 1;  // set template version
  await new (model(req))(template).save((err, data) => {
    if (err) next (err);
    db.log(req, req.params.collection + '_templates', {_id: data._id}, data.toObject());
    res.json(TemplateValidate.output(data.toObject()));
  });
});


module.exports = router;

function model (req) {  // return right template model
  switch (req.params.collection) {
    case 'condition': return ConditionTemplateModel
    case 'measurement': return MeasurementTemplateModel
    case 'material': return MaterialTemplateModel
  }
}