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

import MaterialValidate from './validate/material';
import MaterialModel from '../models/material'
import SampleModel from '../models/sample';
import MaterialGroupModel from '../models/material_groups';
import MaterialSupplierModel from '../models/material_suppliers';
import IdValidate from './validate/id';
import res400 from './validate/res400';
import mongoose from 'mongoose';
import db from '../db';
import MaterialTemplateModel from '../models/material_template';
import ParametersValidate from './validate/parameters';
import globals from '../globals';



const router = express.Router();

router.get('/materials', (req, res, next) => {
  if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'all')) return;

  const {error, value: filters} = MaterialValidate.query(req.query);
  if (error) return res400(error, res);

  let conditions;

  if (filters.hasOwnProperty('status')) {
    if(filters.status === 'all') {
      conditions = {$or: [{status: globals.status.val}, {status: globals.status.new}]}
    }
    else {
      conditions = {status: filters.status};
    }
  }
  else {  // default
    conditions = {status: globals.status.val};
  }

  MaterialModel.find(conditions).populate('group_id').populate('supplier_id').lean().exec((err, data) => {
    if (err) return next(err);

    // validate all and filter null values from validation errors
    res.json(_.compact(data.map(e => MaterialValidate.output(e))));
  });
});

router.get(`/materials/:state(${globals.status.new}|${globals.status.del})`, (req, res, next) => {
  if (!req.auth(res, ['dev', 'admin'], 'basic')) return;

  MaterialModel.find({status: req.params.state}).populate('group_id').populate('supplier_id')
    .lean().exec((err, data) => {
    if (err) return next(err);

    // validate all and filter null values from validation errors
    res.json(_.compact(data.map(e => MaterialValidate.output(e))));
  });
});

router.get('/material/' + IdValidate.parameter(), (req, res, next) => {
  if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'all')) return;

  MaterialModel.findById(req.params.id).populate('group_id').populate('supplier_id').lean().exec((err, data: any) => {
    if (err) return next(err);

    if (!data) {
      return res.status(404).json({status: 'Not found'});
    }

    // deleted materials only available for dev/admin
    if (data.status === globals.status.del && !req.auth(res, ['dev', 'admin'], 'all')) return;
    res.json(MaterialValidate.output(data));
  });
});

router.put('/material/' + IdValidate.parameter(), (req, res, next) => {
  if (!req.auth(res, ['write', 'dev', 'admin'], 'basic')) return;

  let {error, value: material} = MaterialValidate.input(req.body, 'change');
  if (error) return res400(error, res);

  MaterialModel.findById(req.params.id).lean().exec(async (err, materialData: any) => {
    if (!materialData) {
      return res.status(404).json({status: 'Not found'});
    }
    if (materialData.status === 'deleted') {
      return res.status(403).json({status: 'Forbidden'});
    }
    if (material.hasOwnProperty('name') && material.name !== materialData.name) {
      if (!await nameCheck(material, res, next)) return;
    }
    if (material.hasOwnProperty('group')) {
      material = await groupResolve(material, req, next);
      if (!material) return;
    }
    if (material.hasOwnProperty('supplier')) {
      material = await supplierResolve(material, req, next);
      if (!material) return;
    }
    if (material.hasOwnProperty('properties')) {
      if (!await propertiesCheck(material.properties, 'change', res, next,
        materialData.properties.material_template.toString() !== material.properties.material_template)) return;
    }

    // check for changes
    if (!_.isEqual(_.pick(IdValidate.stringify(materialData), _.keys(material)), IdValidate.stringify(material))) {
      material.status = globals.status.new;  // set status to new
    }

    await MaterialModel.findByIdAndUpdate(req.params.id, material, {new: true})
      .log(req).populate('group_id').populate('supplier_id').lean().exec((err, data) => {
      if (err) return next(err);
      res.json(MaterialValidate.output(data));
    });
  });
});

router.delete('/material/' + IdValidate.parameter(), (req, res, next) => {
  if (!req.auth(res, ['write', 'dev', 'admin'], 'basic')) return;

  // check if there are still samples referencing this material
  SampleModel.find({'material_id': new mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data) => {
    if (err) return next(err);
    if (data.length) {
      return res.status(400).json({status: 'Material still in use'});
    }
    MaterialModel.findByIdAndUpdate(req.params.id, {status: globals.status.del})
      .log(req).populate('group_id').populate('supplier_id').lean().exec((err, data) => {
      if (err) return next(err);
      if (data) {
        res.json({status: 'OK'});
      }
      else {
        res.status(404).json({status: 'Not found'});
      }
    });
  });
});

router.put('/material/restore/' + IdValidate.parameter(), (req, res, next) => {
  if (!req.auth(res, ['dev', 'admin'], 'basic')) return;

  setStatus(globals.status.new, req, res, next);
});

router.put('/material/validate/' + IdValidate.parameter(), (req, res, next) => {
  if (!req.auth(res, ['dev', 'admin'], 'basic')) return;

  setStatus(globals.status.val, req, res, next);
});

router.post('/material/new', async (req, res, next) => {
  if (!req.auth(res, ['write', 'dev', 'admin'], 'basic')) return;

  let {error, value: material} = MaterialValidate.input(req.body, 'new');
  if (error) return res400(error, res);

  if (!await nameCheck(material, res, next)) return;
  material = await groupResolve(material, req, next);
  if (!material) return;
  material = await supplierResolve(material, req, next);
  if (!material) return;
  if (!await propertiesCheck(material.properties, 'new', res, next)) return;

  material.status = globals.status.new;  // set status to new
  await new MaterialModel(material).save(async (err, data) => {
    if (err) return next(err);
    db.log(req, 'materials', {_id: data._id}, data.toObject());
    await data.populate('group_id').populate('supplier_id').execPopulate().catch(err => next(err));
    if (data instanceof Error) return;
    res.json(MaterialValidate.output(data.toObject()));
  });
});

router.get('/material/groups', (req, res, next) => {
  if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'all')) return;

  MaterialGroupModel.find().lean().exec((err, data: any) => {
    if (err) return next(err);

    // validate all and filter null values from validation errors
    res.json(_.compact(data.map(e => MaterialValidate.outputGroups(e.name))));
  });
});

router.get('/material/suppliers', (req, res, next) => {
  if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'all')) return;

  MaterialSupplierModel.find().lean().exec((err, data: any) => {
    if (err) return next(err);

    // validate all and filter null values from validation errors
    res.json(_.compact(data.map(e => MaterialValidate.outputSuppliers(e.name))));
  });
});


module.exports = router;


async function nameCheck (material, res, next) {  // check if name was already taken
  const materialData = await MaterialModel.findOne({name: material.name}).lean().exec().catch(err => next(err)) as any;
  if (materialData instanceof Error) return false;
  if (materialData) {  // could not find material_id
    res.status(400).json({status: 'Material name already taken'});
    return false;
  }
  return true;
}

async function groupResolve (material, req, next) {
  const groupData = await MaterialGroupModel.findOneAndUpdate(
    {name: material.group},
    {name: material.group},
    {upsert: true, new: true}
    ).log(req).lean().exec().catch(err => next(err)) as any;
  if (groupData instanceof Error) return false;
  material.group_id = groupData._id;
  delete material.group;
  return material;
}

async function supplierResolve (material, req, next) {
  const supplierData = await MaterialSupplierModel.findOneAndUpdate(
    {name: material.supplier},
    {name: material.supplier},
    {upsert: true, new: true}
    ).log(req).lean().exec().catch(err => next(err)) as any;
  if (supplierData instanceof Error) return false;
  material.supplier_id = supplierData._id;
  delete material.supplier;
  return material;
}

// validate material properties, returns false if invalid, otherwise template data
async function propertiesCheck (properties, param, res, next, checkVersion = true) {
  if (!properties.material_template || !IdValidate.valid(properties.material_template)) {  // template id not found
    res.status(400).json({status: 'Material template not available'});
    return false;
  }
  const materialData = await MaterialTemplateModel.findById(properties.material_template)
    .lean().exec().catch(err => next(err)) as any;
  if (materialData instanceof Error) return false;
  if (!materialData) {  // template not found
    res.status(400).json({status: 'Material template not available'});
    return false;
  }

  if (checkVersion) {
    // get all template versions and check if given is latest
    const materialVersions = await MaterialTemplateModel.find({first_id: materialData.first_id}).sort({version: -1})
      .lean().exec().catch(err => next(err)) as any;
    if (materialVersions instanceof Error) return false;
    if (properties.material_template !== materialVersions[0]._id.toString()) {  // template not latest
      res.status(400).json({status: 'Old template version not allowed'});
      return false;
    }
  }

  // validate parameters
  const {error, value} = ParametersValidate
    .input(_.omit(properties, 'material_template'), materialData.parameters, param);
  if (error) {res400(error, res); return false;}
  Object.keys(value).forEach(key => {
    properties[key] = value[key];
  });
  return materialData;
}

function setStatus (status, req, res, next) {  // set measurement status
  MaterialModel.findByIdAndUpdate(req.params.id, {status: status}).log(req).lean().exec((err, data) => {
    if (err) return next(err);

    if (!data) {
      return res.status(404).json({status: 'Not found'});
    }
    res.json({status: 'OK'});
  });
}