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

import SampleValidate from './validate/sample';
import NoteFieldValidate from './validate/note_field';
import res400 from './validate/res400';
import SampleModel from '../models/sample'
import MeasurementModel from '../models/measurement';
import MeasurementTemplateModel from '../models/measurement_template';
import MaterialModel from '../models/material';
import NoteModel from '../models/note';
import NoteFieldModel from '../models/note_field';
import IdValidate from './validate/id';
import mongoose from 'mongoose';
import ConditionTemplateModel from '../models/condition_template';
import ParametersValidate from './validate/parameters';
import db from '../db';
import csv from '../helpers/csv';
import flatten from '../helpers/flatten';
import globals from '../globals';


const router = express.Router();

// TODO: do not use streaming for spectrum filenames


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

  const {error, value: filters} = SampleValidate.query(req.query, ['dev', 'admin'].indexOf(req.authDetails.level) >= 0);
  console.log(error);
  if (error) return res400(error, res);
  console.log(filters.filters);

  // spectral data and csv not allowed for read/write users
  if ((filters.fields.find(e => e.indexOf('.' + globals.spectrum.dpt) >= 0) || filters.output !== 'json') &&
    !req.auth(res, ['dev', 'admin'], 'all')) return;

  // evaluate sort parameter from 'color-asc' to ['color', 1]
  filters.sort = filters.sort.split('-');
  filters.sort[0] = filters.sort[0] === 'added' ? '_id' : filters.sort[0];  // route added sorting criteria to _id
  filters.sort[1] = filters.sort[1] === 'desc' ? -1 : 1;
  if (!filters['to-page']) {  // set to-page default
    filters['to-page'] = 0;
  }
  const addedFilter = filters.filters.find(e => e.field === 'added');
  if (addedFilter) {  // convert added filter to object id
    filters.filters.splice(filters.filters.findIndex(e => e.field === 'added'), 1);
    if (addedFilter.mode === 'in') {
      const v = [];  // query value
      addedFilter.values.forEach(value => {
        const date = [new Date(value).setHours(0,0,0,0), new Date(value).setHours(23,59,59,999)];
        v.push({$and: [{ _id: { '$gte': dateToOId(date[0])}}, { _id: { '$lte': dateToOId(date[1])}}]});
      });
      filters.filters.push({mode: 'or', field: '_id', values: v});
    }
    else if (addedFilter.mode === 'nin') {
      addedFilter.values = addedFilter.values.sort();
      const v = [];  // query value

      for (let i = 0; i <= addedFilter.values.length; i ++) {
        v[i] = {$and: []};
        if (i > 0) {
          const date = new Date(addedFilter.values[i - 1]).setHours(23,59,59,999);
          v[i].$and.push({ _id: { '$gt': dateToOId(date)}}) ;
        }
        if (i < addedFilter.values.length) {
          const date = new Date(addedFilter.values[i]).setHours(0,0,0,0);
          v[i].$and.push({ _id: { '$lt': dateToOId(date)}}) ;
        }
      }
      filters.filters.push({mode: 'or', field: '_id', values: v});
    }
    else {
      // start and end of day
      const date = [new Date(addedFilter.values[0]).setHours(0,0,0,0),
        new Date(addedFilter.values[0]).setHours(23,59,59,999)];
      if (addedFilter.mode === 'lt') {  // lt start
        filters.filters.push({mode: 'lt', field: '_id', values: [dateToOId(date[0])]});
      }
      if (addedFilter.mode === 'eq' || addedFilter.mode === 'lte') {  // lte end
        filters.filters.push({mode: 'lte', field: '_id', values: [dateToOId(date[1])]});
      }
      if (addedFilter.mode === 'gt') {  // gt end
        filters.filters.push({mode: 'gt', field: '_id', values: [dateToOId(date[1])]});
      }
      if (addedFilter.mode === 'eq' || addedFilter.mode === 'gte') {  // gte start
        filters.filters.push({mode: 'gte', field: '_id', values: [dateToOId(date[0])]});
      }
      if (addedFilter.mode === 'ne') {
        filters.filters.push({mode: 'or', field: '_id',
          values: [{ _id: { '$lt': dateToOId(date[0])}}, { _id: { '$gt': dateToOId(date[1])}}]});
      }
    }
  }

  const sortFilterKeys = filters.filters.map(e => e.field);

  let collection;
  const query = [];
  let queryPtr = query;
  queryPtr.push({$match: {$and: []}});

  if (filters.sort[0].indexOf('measurements.') >= 0) {  // sorting with measurements as starting collection
    collection = MeasurementModel;
    const [,measurementName, measurementParam] = filters.sort[0].split('.');
    const measurementTemplates = await MeasurementTemplateModel.find({name: measurementName})
      .lean().exec().catch(err => {next(err);});
    if (measurementTemplates instanceof Error) return;
    if (!measurementTemplates) {
      return res.status(400).json({status: 'Invalid body format', details: filters.sort[0] + ' not found'});
    }
    let sortStartValue = null;
    if (filters['from-id']) {  // from-id specified, fetch values for sorting
      const fromSample = await MeasurementModel.findOne({sample_id: mongoose.Types.ObjectId(filters['from-id'])})
        .lean().exec().catch(err => {next(err);});
      if (fromSample instanceof Error) return;
      if (!fromSample) {
        return res.status(400).json({status: 'Invalid body format', details: 'from-id not found'});
      }
      sortStartValue = fromSample.values[measurementParam];
    }
    // find measurements to sort
    queryPtr[0].$match.$and.push({measurement_template: {$in: measurementTemplates.map(e => e._id)}});
    if (filters.filters.find(e => e.field === filters.sort[0])) {  // sorted measurement should also be filtered
      queryPtr[0].$match.$and.push(...filterQueries(filters.filters.filter(e => e.field === filters.sort[0])
        .map(e => {e.field = 'values.' + e.field.split('.')[2]; return e; })));
    }
    queryPtr.push(
      ...sortQuery(filters, ['values.' + measurementParam, 'sample_id'], sortStartValue),  // sort measurements
      {$replaceRoot: {newRoot: {measurement: '$$ROOT'}}},  // fetch samples and restructure them to fit sample structure
      {$lookup: {from: 'samples', localField: 'measurement.sample_id', foreignField: '_id', as: 'sample'}},
      {$match: statusQuery(filters, 'sample.status')},  // filter out wrong status once samples were added
      {$addFields: {['sample.' + measurementName]: '$measurement.values'}},  // more restructuring
      {$replaceRoot: {newRoot: {$mergeObjects: [{$arrayElemAt: ['$sample', 0]}, {}]}}}
    );
  }
  else {  // sorting with samples as starting collection
    collection = SampleModel;
    queryPtr[0].$match.$and.push(statusQuery(filters, 'status'));

    // sorting for sample keys
    if (SampleValidate.sampleKeys.indexOf(filters.sort[0]) >= 0 || /condition\./.test(filters.sort[0])) {
      let sortStartValue = null;
      if (filters['from-id']) {  // from-id specified
        const fromSample = await SampleModel.findById(filters['from-id']).lean().exec().catch(err => {
          next(err);
        });
        if (fromSample instanceof Error) return;
        if (!fromSample) {
          return res.status(400).json({status: 'Invalid body format', details: 'from-id not found'});
        }
        sortStartValue = fromSample[filters.sort[0]];
      }
      queryPtr.push(...sortQuery(filters, [filters.sort[0], '_id'], sortStartValue));
    }
    else {  // add sort key to list to add field later
      sortFilterKeys.push(filters.sort[0]);
    }
  }

  addFilterQueries(queryPtr, filters.filters.filter(
    e => (SampleValidate.sampleKeys.indexOf(e.field) >= 0) || /condition\./.test(e.field))
  );  // sample filters

  let materialQuery = [];  // put material query together separate first to reuse for first-id
  let materialAdded = false;
  if (sortFilterKeys.find(e => /material\./.test(e))) {  //  add material fields
    materialAdded = true;
    materialQuery.push(  // add material properties
      {$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}},
      {$addFields: {material: {$arrayElemAt: ['$material', 0]}}}
    );
    const baseMFilters = sortFilterKeys.filter(e => /material\./.test(e))
      .filter(e => ['material.supplier', 'material.group', 'material.number'].indexOf(e) < 0);
    // base material filters
    addFilterQueries(materialQuery, filters.filters.filter(e => baseMFilters.indexOf(e.field) >= 0));
    if (sortFilterKeys.find(e => e === 'material.supplier')) {  // add supplier if needed
      materialQuery.push(
        {$lookup: {
          from: 'material_suppliers', localField: 'material.supplier_id', foreignField: '_id', as: 'material.supplier'}
        },
        {$addFields: {'material.supplier': {$arrayElemAt: ['$material.supplier.name', 0]}}}
      );
    }
    if (sortFilterKeys.find(e => e === 'material.group')) {  // add group if needed
      materialQuery.push(
        {$lookup: {
          from: 'material_groups', localField: 'material.group_id', foreignField: '_id', as: 'material.group' }
        },
        {$addFields: {'material.group': { $arrayElemAt: ['$material.group.name', 0]}}}
      );
    }
    const specialMFilters = sortFilterKeys.filter(e => /material\./.test(e))
      .filter(e => ['material.supplier', 'material.group', 'material.number'].indexOf(e) >= 0);
    // base material filters
    addFilterQueries(materialQuery, filters.filters.filter(e => specialMFilters.indexOf(e.field) >= 0));
    queryPtr.push(...materialQuery);
    if (/material\./.test(filters.sort[0])) {  // sort by material key
      let sortStartValue = null;
      if (filters['from-id']) {  // from-id specified
        const fromSample = await SampleModel.aggregate(
          [{$match: {_id: mongoose.Types.ObjectId(filters['from-id'])}}, ...materialQuery]
        ).exec().catch(err => {next(err);});
        if (fromSample instanceof Error) return;
        if (!fromSample) {
          return res.status(400).json({status: 'Invalid body format', details: 'from-id not found'});
        }
        const filterKey = filters.sort[0].split('.');
        if (filterKey.length === 2) {
          sortStartValue = fromSample[0][filterKey[0]][filterKey[1]];
        }
        else {
          sortStartValue = fromSample[0][filterKey[0]];
        }
      }
      queryPtr.push(...sortQuery(filters, [filters.sort[0], '_id'], sortStartValue));
    }
  }

  const measurementFilterFields = _.uniq(sortFilterKeys.filter(e => /measurements\./.test(e))
    .map(e => e.split('.')[1]));  // filter measurement names and remove duplicates from parameters
  if (sortFilterKeys.find(e => /measurements\./.test(e))) {  //  add measurement fields
    const measurementTemplates = await MeasurementTemplateModel.find({name: {$in: measurementFilterFields}})
      .lean().exec().catch(err => {next(err);});
    if (measurementTemplates instanceof Error) return;
    if (measurementTemplates.length < measurementFilterFields.length) {
      return res.status(400).json({status: 'Invalid body format', details: 'Measurement key not found'});
    }
    queryPtr.push({$lookup: {
        from: 'measurements', let: {sId: '$_id'},
        pipeline: [{$match: {$expr: {$and: [
            {$eq: ['$sample_id', '$$sId']},
            {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
          ]}}}
        ],
        as: 'measurements'
    }});
    const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => {
      if (s.hasOwnProperty(e.name)) {
        s[e.name].push(e);
      }
      else {
        s[e.name] = [e];
      }
      return s;
    }, {});
    Object.values(groupedMeasurementTemplates).forEach(templates => {
      addMeasurements(queryPtr, templates);
    });
    addFilterQueries(queryPtr, filters.filters
      .filter(e => sortFilterKeys.filter(e => /measurements\./.test(e)).indexOf(e.field) >= 0)
      .map(e => {e.field = e.field.replace('measurements.', ''); return e; })
    );  // measurement filters
  }

  // count total number of items before $skip and $limit, only works when from-id is not specified and spectra are not
  // included
  if (!filters.fields.find(e =>
    e.indexOf(globals.spectrum.spectrum + '.' + globals.spectrum.dpt) >= 0) && !filters['from-id']
  ) {
    queryPtr.push({$facet: {count: [{$count: 'count'}], samples: []}});
    queryPtr = queryPtr[queryPtr.length - 1].$facet.samples;  // add rest of aggregation pipeline into $facet
  }

  // paging
  if (filters['to-page']) {
    // number to skip, if going back pages, one page has to be skipped less but on sample more
    queryPtr.push({$skip: Math.abs(filters['to-page'] + Number(filters['to-page'] < 0)) * filters['page-size'] +
      Number(filters['to-page'] < 0)})
  }
  if (filters['page-size']) {
    queryPtr.push({$limit: filters['page-size']});
  }

  const fieldsToAdd = filters.fields.filter(e =>   // fields to add
    sortFilterKeys.indexOf(e) < 0                      // field was not in filter
    && e !== filters.sort[0]                           // field was not in sort
  );

  if (fieldsToAdd.find(e => e === 'notes')) {  // add notes
    queryPtr.push(
      {$lookup: {from: 'notes', localField: 'note_id', foreignField: '_id', as: 'notes'}},
      {$addFields: {notes: { $arrayElemAt: ['$notes', 0]}}}
    );
  }

  if (fieldsToAdd.find(e => /material\./.test(e)) && !materialAdded) {  // add material, was not added already
    queryPtr.push(
      {$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}},
      {$addFields: {material: { $arrayElemAt: ['$material', 0]}}}
    );
  }
  if (fieldsToAdd.indexOf('material.supplier') >= 0) {  // add supplier if needed
    queryPtr.push(
      {$lookup: {
        from: 'material_suppliers', localField: 'material.supplier_id', foreignField: '_id', as: 'material.supplier'
      }},
      {$addFields: {'material.supplier': {$arrayElemAt: ['$material.supplier.name', 0]}}}
    );
  }
  if (fieldsToAdd.indexOf('material.group') >= 0) {  // add group if needed
    queryPtr.push(
      {$lookup: {
        from: 'material_groups', localField: 'material.group_id', foreignField: '_id', as: 'material.group'
      }},
      {$addFields: {'material.group': { $arrayElemAt: ['$material.group.name', 0]}}}
    );
  }

  let measurementFieldsFields: string[] = _.uniq(
    fieldsToAdd.filter(e => /measurements\./.test(e)).map(e => e.split('.')[1])
  );  // filter measurement names and remove duplicates from parameters
  if (fieldsToAdd.find(e => /measurements\./.test(e))) {  // add measurement fields
    const measurementTemplates = await MeasurementTemplateModel.find({name: {$in: measurementFieldsFields}})
      .lean().exec().catch(err => {next(err);});
    if (measurementTemplates instanceof Error) return;
    if (measurementTemplates.length < measurementFieldsFields.length) {
      return res.status(400).json({status: 'Invalid body format', details: 'Measurement key not found'});
    }
    // use different lookup methods with and without dpt for the best performance
    if (fieldsToAdd.find(e => e.indexOf(globals.spectrum.spectrum + '.' + globals.spectrum.dpt) >= 0)) {  // with dpt
      queryPtr.push(
        {$lookup: {from: 'measurements', localField: '_id', foreignField: 'sample_id', as: 'measurements'}}
      );
    }
    else {
      queryPtr.push({$lookup: {
          from: 'measurements', let: {sId: '$_id'},
          pipeline: [{$match: {$expr: {$and: [
              {$eq: ['$sample_id', '$$sId']},
              {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
            ]}}},
            {$project: _.merge(
              filters.fields.filter(e => /measurements\./.test(e))
                .map(e => 'values.' + e.split('.')[2]).reduce((s, e) => {s[e] = true; return s; }, {}),
              {measurement_template: true, status: true, sample_id: true}
            )}
          ],
          as: 'measurements'
        }});
    }
    const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => {
      if (s.hasOwnProperty(e.name)) {
        s[e.name].push(e);
      }
      else {
        s[e.name] = [e];
      }
      return s;
    }, {});
    Object.values(groupedMeasurementTemplates).forEach(templates => {
      addMeasurements(queryPtr, templates);
    });
    queryPtr.push({$project: {measurements: 0}});
  }

  const projection = filters.fields.map(e => e.replace('measurements.', ''))
    .reduce((s, e) => {s[e] = true; return s; }, {});
  if (filters.fields.indexOf('_id') < 0 && filters.fields.indexOf('added') < 0) {  // disable _id explicitly
    projection._id = false;
  }
  queryPtr.push({$project: projection});
  console.log(JSON.stringify(query));
  // use streaming when including spectrum files
  if (!fieldsToAdd.find(e => e.indexOf(globals.spectrum.spectrum + '.' + globals.spectrum.dpt) >= 0)) {
    collection.aggregate(query).allowDiskUse(true).exec((err, data) => {
      if (err) return next(err);
      if (data[0] && data[0].count) {
        res.header('x-total-items', data[0].count.length > 0 ? data[0].count[0].count : 0);
        res.header('Access-Control-Expose-Headers', 'x-total-items');
        data = data[0].samples;
      }
      if (filters.fields.indexOf('added') >= 0) {  // add added date
        data.map(e => {
          e.added = e._id.getTimestamp();
          if (filters.fields.indexOf('_id') < 0) {
            delete e._id;
          }
          return e
        });
      }
      if (filters['to-page'] < 0) {
        data.reverse();
      }
      const measurementFields = _.uniq(
        [filters.sort[0].split('.')[1],
        ...measurementFilterFields, ...measurementFieldsFields]
      );
      if (filters.output === 'csv') {  // output as csv
        csv(_.compact(data.map(e => SampleValidate.output(e, 'refs', measurementFields))), (err, data) => {
          if (err) return next(err);
          res.set('Content-Type', 'text/csv');
          res.set('Content-Disposition', 'attachment; filename="samples.csv"');
          res.send(data);
        });
      }
      else if (filters.output === 'flatten') {
        res.json(_.compact(data.map(e => flatten(SampleValidate.output(e, 'refs', measurementFields), true))));
      }
      else {  // validate all and filter null values from validation errors
        res.json(_.compact(data.map(e => SampleValidate.output(e, 'refs', measurementFields))));
      }
    });
  }
  else {
    res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
    res.write('[');
    let count = 0;
    const stream = collection.aggregate(query).allowDiskUse(true).cursor().exec();
    stream.on('data', data => {
      if (filters.fields.indexOf('added') >= 0) {  // add added date
        data.added = data._id.getTimestamp();
        if (filters.fields.indexOf('_id') < 0) {
          delete data._id;
        }
      }
      if (filters.output === 'flatten') {
        data = flatten(data, true);
      }
      res.write((count === 0 ? '' : ',\n') + JSON.stringify(data)); count ++;
    });
    stream.on('error', err => {
      console.error(err);
    });
    stream.on('close', () => {
      res.write(']');
      res.end();
    });
  }
});

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

  SampleModel.find({status: req.params.state}).lean().exec((err, data) => {
    if (err) return next(err);
    // validate all and filter null values from validation errors
    res.json(_.compact(data.map(e => SampleValidate.output(e))));
  });
});

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

  SampleModel.estimatedDocumentCount((err, data) => {
    if (err) return next(err);
    res.json({count: data});
  });
});

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

  SampleModel.findById(req.params.id).populate('material_id').populate('user_id', 'name').populate('note_id')
    .exec(async (err, sampleData: any) => {
      if (err) return next(err);
    await sampleReturn(sampleData, req, res, next);
  });
});

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

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

  SampleModel.findById(req.params.id).lean().exec(async (err, sampleData: any) => {  // check if id exists
    if (err) return next(err);
    if (!sampleData) {
      return res.status(404).json({status: 'Not found'});
    }
    if (sampleData.status === globals.status.del) {
      return res.status(403).json({status: 'Forbidden'});
    }

    // only dev and admin are allowed to edit other user's data
    if (sampleData.user_id.toString() !== req.authDetails.id && !req.auth(res, ['dev', 'admin'], 'basic')) return;
    if (sample.hasOwnProperty('material_id')) {
      if (!await materialCheck(sample, res, next)) return;
    }
    // do not execute check if condition is and was empty
    if (sample.hasOwnProperty('condition') && !(_.isEmpty(sample.condition) && _.isEmpty(sampleData.condition))) {
      if (!await conditionCheck(sample.condition, 'change', res, next,
        !(sampleData.condition.condition_template &&
        sampleData.condition.condition_template.toString() === sample.condition.condition_template))) return;
    }

    if (sample.hasOwnProperty('notes')) {
      let newNotes = true;
      if (sampleData.note_id !== null) {  // old notes data exists
        const data = await NoteModel.findById(sampleData.note_id).lean().exec().catch(err => {next(err);}) as any;
        if (data instanceof Error) return;
        // check if notes were changed
        newNotes = !_.isEqual(_.pick(IdValidate.stringify(data), _.keys(sample.notes)), sample.notes);
        if (newNotes) {
          if (data.hasOwnProperty('custom_fields')) {  // update note_fields
            customFieldsChange(Object.keys(data.custom_fields), -1, req);
          }
          await NoteModel.findByIdAndDelete(sampleData.note_id).log(req).lean().exec(err => {  // delete old notes
            if (err) return console.error(err);
          });
        }
      }

      if (_.keys(sample.notes).length > 0 && newNotes) {  // save new notes
        if (!await sampleRefCheck(sample, res, next)) return;
        // new custom_fields
        if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) {
          customFieldsChange(Object.keys(sample.notes.custom_fields), 1, req);
        }
        let data = await new NoteModel(sample.notes).save().catch(err => { return next(err)});  // save new notes
        db.log(req, 'notes', {_id: data._id}, data.toObject());
        delete sample.notes;
        sample.note_id = data._id;
      }
    }

    // check for changes
    if (!_.isEqual(_.pick(IdValidate.stringify(sampleData), _.keys(sample)), _.omit(sample, ['notes']))) {
      sample.status = globals.status.new;
    }

    await SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).log(req).lean().exec((err, data: any) => {
      if (err) return next(err);
      res.json(SampleValidate.output(data));
    });

  });
});

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

  SampleModel.findById(req.params.id).lean().exec(async (err, sampleData: any) => {  // check if id exists
    if (err) return next(err);
    if (!sampleData) {
      return res.status(404).json({status: 'Not found'});
    }

    // only dev and admin are allowed to edit other user's data
    if (sampleData.user_id.toString() !== req.authDetails.id && !req.auth(res, ['dev', 'admin'], 'basic')) return;

    // set sample status
    await SampleModel.findByIdAndUpdate(req.params.id, {status:'deleted'}).log(req).lean().exec(err => {
      if (err) return next(err);

      // set status of associated measurements also to deleted
      MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: globals.status.del})
        .log(req).lean().exec(err => {
        if (err) return next(err);

        if (sampleData.note_id !== null) {  // handle notes
          NoteModel.findById(sampleData.note_id).lean().exec((err, data: any) => {  // find notes to update note_fields
            if (err) return next(err);
            if (data.hasOwnProperty('custom_fields')) {  // update note_fields
              customFieldsChange(Object.keys(data.custom_fields), -1, req);
            }
            res.json({status: 'OK'});
          });
        }
        else {
          res.json({status: 'OK'});
        }
      });
    });
  });
});

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

  SampleModel.findOne({number: req.params.number}).populate('material_id').populate('user_id', 'name')
    .populate('note_id').exec(async (err, sampleData: any) => {
    if (err) return next(err);
    await sampleReturn(sampleData, req, res, next);
  });
});

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

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

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

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

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

  if (!req.body.hasOwnProperty('condition')) {  // add empty condition if not specified
    req.body.condition = {};
  }

  const {error, value: sample} =
    SampleValidate.input(req.body, 'new' + (req.authDetails.level === 'admin' ? '-admin' : ''));
  if (error) return res400(error, res);

  if (!await materialCheck(sample, res, next)) return;
  if (!await sampleRefCheck(sample, res, next)) return;

  // new custom_fields
  if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) {
    customFieldsChange(Object.keys(sample.notes.custom_fields), 1, req);
  }

  if (!_.isEmpty(sample.condition)) {  // do not execute check if condition is empty
    if (!await conditionCheck(sample.condition, 'change', res, next)) return;
  }

  sample.status = globals.status.new;  // set status to new
  if (sample.hasOwnProperty('number')) {
    if (!await numberCheck(sample, res, next)) return;
  }
  else {
    sample.number = await numberGenerate(sample, req, res, next);
  }
  if (!sample.number) return;

  await new NoteModel(sample.notes).save((err, data) => {  // save notes
    if (err) return next(err);
    db.log(req, 'notes', {_id: data._id}, data.toObject());
    delete sample.notes;
    sample.note_id = data._id;
    sample.user_id = req.authDetails.id;

    new SampleModel(sample).save((err, data) => {
      if (err) return next(err);
      db.log(req, 'samples', {_id: data._id}, data.toObject());
      res.json(SampleValidate.output(data.toObject()));
    });
  });
});

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

  NoteFieldModel.find({}).lean().exec((err, data) => {
    if (err) return next(err);
    // validate all and filter null values from validation errors
    res.json(_.compact(data.map(e => NoteFieldValidate.output(e))));
  })
});


module.exports = router;

// store the highest generated number for each location to avoid duplicate numbers
const numberBuffer: {[location: string]: number} = {};

// generate number in format Location32, returns false on error
async function numberGenerate (sample, req, res, next) {
  const sampleData = await SampleModel
    .aggregate([
      {$match: {number: new RegExp('^' + req.authDetails.location + '[0-9]+$', 'm')}},
      // {$addFields: {number2: {$toDecimal: {$arrayElemAt: [{$split: [{$arrayElemAt:
      //   [{$split: ['$number', 'Rng']}, 1]}, '_']}, 0]}}}},  // not working with MongoDb 3.6
      {$addFields: {sortNumber: {$let: {
        vars: {tmp: {$concat: ['000000000000000000000000000000',
              {$arrayElemAt: [{$split: [{$arrayElemAt: [{$split: ['$number', 'Rng']}, 1]}, '_']}, 0]}]}},
        in: {$substrCP: ['$$tmp', {$subtract: [{$strLenCP: '$$tmp'}, 30]}, {$strLenCP: '$$tmp'}]}
      }}}},
      {$sort: {sortNumber: -1}},
      {$limit: 1}
    ])
    .exec()
    .catch(err => next(err));
  if (sampleData instanceof Error) return false;
  let number = (sampleData[0] ? Number(sampleData[0].number.replace(/[^0-9]+/g, '')) : 0);
  if (numberBuffer[req.authDetails.location] && numberBuffer[req.authDetails.location] >= number) {
    number = numberBuffer[req.authDetails.location];
  }
  number ++;
  numberBuffer[req.authDetails.location] = number;
  return req.authDetails.location + number;
}

async function numberCheck(sample, res, next) {
  const sampleData = await SampleModel.findOne({number: sample.number})
    .lean().exec().catch(err => {next(err); return false;});
  if (sampleData) {  // found entry with sample number
    res.status(400).json({status: 'Sample number already taken'});
    return false
  }
  return true;
}

// validate material_id and color, returns false if invalid
async function materialCheck (sample, res, next) {
  const materialData = await MaterialModel.findById(sample.material_id).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 not available'});
    return false;
  }
  return true;
}

// validate treatment template, returns false if invalid, otherwise template data
async function conditionCheck (condition, param, res, next, checkVersion = true) {
  if (!condition.condition_template || !IdValidate.valid(condition.condition_template)) {  // template id not found
    res.status(400).json({status: 'Condition template not available'});
    return false;
  }
  const conditionData = await ConditionTemplateModel.findById(condition.condition_template)
    .lean().exec().catch(err => next(err)) as any;
  if (conditionData instanceof Error) return false;
  if (!conditionData) {  // template not found
    res.status(400).json({status: 'Condition template not available'});
    return false;
  }

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

  // validate parameters
  const {error, value: ignore} =
    ParametersValidate.input(_.omit(condition, 'condition_template'), conditionData.parameters, param);
  if (error) {res400(error, res); return false;}
  return conditionData;
}

function sampleRefCheck (sample, res, next) {  // validate sample_references, resolves false for invalid reference
  return new Promise(resolve => {
    // there are sample_references
    if (sample.notes.hasOwnProperty('sample_references') && sample.notes.sample_references.length > 0) {
      let referencesCount = sample.notes.sample_references.length;  // count to keep track of running async operations

      sample.notes.sample_references.forEach(reference => {
        SampleModel.findById(reference.sample_id).lean().exec((err, data) => {
          if (err) {next(err); resolve(false)}
          if (!data) {
            res.status(400).json({status: 'Sample reference not available'});
            return resolve(false);
          }
          referencesCount --;
          if (referencesCount <= 0) {  // all async requests done
            resolve(true);
          }
        });
      });
    }
    else {
      resolve(true);
    }
  });
}

function customFieldsChange (fields, amount, req) {  // update custom_fields and respective quantities
  fields.forEach(field => {
    NoteFieldModel.findOneAndUpdate({name: field}, {$inc: {qty: amount}} as any, {new: true})
      .log(req).lean().exec((err, data: any) => {  // check if field exists
      if (err) return console.error(err);
      if (!data) {  // new field
        new NoteFieldModel({name: field, qty: 1}).save((err, data) => {
          if (err) return console.error(err);
          db.log(req, 'note_fields', {_id: data._id}, data.toObject());
        })
      }
      else if (data.qty <= 0) {  // delete document if field is not used anymore
        NoteFieldModel.findOneAndDelete({name: field}).log(req).lean().exec(err => {
          if (err) return console.error(err);
        });
      }
    });
  });
}

function sortQuery(filters, sortKeys, sortStartValue) {  // sortKeys = ['primary key', 'secondary key']
  if (filters['from-id']) {  // from-id specified
    const ssv = sortStartValue !== undefined;  // if value is not given, match for existence
    if ((filters['to-page'] === 0 && filters.sort[1] === 1) || (filters.sort[1] * filters['to-page'] > 0)) {  // asc
      return [
        {$match: {$or: [
          {[sortKeys[0]]: ssv ? {$gt: sortStartValue} : {$exists: true}},
          {$and: [
            {[sortKeys[0]]: ssv ? sortStartValue : {$exists: false}},
            {[sortKeys[1]]: {$gte: new mongoose.Types.ObjectId(filters['from-id'])}}
          ]}
        ]}},
        {$sort: {[sortKeys[0]]: 1, _id: 1}}
      ];
    } else {
      return [
        {$match: {$or: [
          {[sortKeys[0]]: ssv ? {$lt: sortStartValue} : {$exists: false}},
          {$and: [
            {[sortKeys[0]]: ssv ? sortStartValue : {$exists: true}},
            {[sortKeys[1]]: {$lte: new mongoose.Types.ObjectId(filters['from-id'])}}
          ]}
        ]}},
        {$sort: {[sortKeys[0]]: -1, _id: -1}}
      ];
    }
  } else {  // sort from beginning
    return [{$sort: {[sortKeys[0]]: filters.sort[1], [sortKeys[1]]: filters.sort[1]}}];  // set _id as secondary sort
  }
}

function statusQuery(filters, field) {
  return {$or: filters.status.map(e => ({[field]: e}))};
}

function addFilterQueries (queryPtr, filters) {  // returns array of match queries from given filters
  if (filters.length) {
    queryPtr.push({$match: {$and: filterQueries(filters)}});
  }
}

function filterQueries (filters) {
  return filters.map(e => {
    if (e.mode === 'or') {  // allow or queries (needed for $ne added)
      return {['$' + e.mode]: e.values};
    }
    else if (e.mode === 'stringin') {
      return {[e.field]: {['$in']: [new RegExp(e.values[0])]}};
    }
    else {
      // add filter criteria as {field: {$mode: value}}, only use first value when mode is not in/nin
      return {[e.field]: {['$' + e.mode]: (e.mode.indexOf('in') >= 0 ? e.values : e.values[0])}};
    }
  });
}

// add measurements as property [template.name], if one result, array is reduced to direct values. All given templates
// must have the same name
function addMeasurements(queryPtr, templates) {
  queryPtr.push(
    {$addFields: {[templates[0].name]: {$let: {vars: {
      arr: {$filter: {
        input: '$measurements', cond: {$and: [
          {$in: ['$$this.measurement_template', templates.map(e => mongoose.Types.ObjectId(e._id))]},
          {$ne: ['$$this.status', globals.status.del]}
        ]},
      }}},
      in: {$cond: [{$lte: [{$size: '$$arr'}, 1]}, {$arrayElemAt: ['$$arr', 0]}, '$$arr']}
    }}}},
    {$addFields: {[templates[0].name]: {$cond: [
      '$' + templates[0].name + '.values',
      '$' + templates[0].name + '.values',
      templates[0].parameters.reduce((s, e) => {s[e.name] = null; return s;}, {})
    ]}}},
    {$unwind: '$' + templates[0].name}
  );
}

function dateToOId (date) {  // convert date to ObjectId
  return mongoose.Types.ObjectId(Math.floor(date / 1000).toString(16) + '0000000000000000');
}

async function sampleReturn (sampleData, req, res, next) {
  if (sampleData) {
    await sampleData.populate('material_id.group_id').populate('material_id.supplier_id')
      .execPopulate().catch(err => next(err));
    if (sampleData instanceof Error) return;
    sampleData = sampleData.toObject();

    // deleted samples only available for dev/admin
    if (sampleData.status === globals.status.del && !req.auth(res, ['dev', 'admin'], 'all')) return;
    sampleData.material = sampleData.material_id;  // map data to right keys
    sampleData.material.group = sampleData.material.group_id.name;
    sampleData.material.supplier = sampleData.material.supplier_id.name;
    sampleData.user = sampleData.user_id.name;
    sampleData.notes = sampleData.note_id ? sampleData.note_id : {};
    MeasurementModel.find({sample_id: sampleData._id, status: {$ne: 'deleted'}})
      .lean().exec((err, data) => {
      sampleData.measurements = data;
      if (['dev', 'admin'].indexOf(req.authDetails.level) < 0) {  // strip dpt values if not dev or admin
        sampleData.measurements.forEach(measurement => {
          if (measurement.values[globals.spectrum.dpt]) {
            delete measurement.values[globals.spectrum.dpt];
          }
        });
      }
      res.json(SampleValidate.output(sampleData, 'details'));
    });
  }
  else {
    res.status(404).json({status: 'Not found'});
  }
}

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

    if (!data) {
      return res.status(404).json({status: 'Not found'});
    }
    MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status})
      .log(req).lean().exec(err => {
      if (err) return next(err);

      res.json({status: 'OK'});
    });
  });
}