import mongoose from 'mongoose';
import MaterialSupplierModel from '../models/material_suppliers';
import MaterialGroupsModel from '../models/material_groups';
import db from '../db';

const MaterialSchema = new mongoose.Schema({
  name: {type: String, index: {unique: true}},
  supplier_id: {type: mongoose.Schema.Types.ObjectId, ref: MaterialSupplierModel},
  group_id: {type: mongoose.Schema.Types.ObjectId, ref: MaterialGroupsModel},
  properties: mongoose.Schema.Types.Mixed,
  numbers: [String],
  status: String
}, {minimize: false});

// changelog query helper
MaterialSchema.query.log = function <Q extends mongoose.DocumentQuery<any, any>> (req) {
  db.log(req, this);
  return this;
}
MaterialSchema.index({supplier_id: 1});
MaterialSchema.index({group_id: 1});

export default mongoose.model<any, mongoose.Model<any, any>>('material', MaterialSchema);