import mongoose from 'mongoose';

import MaterialModel from './material';
import NoteModel from './note';
import UserModel from './user';
import db from '../db';

const SampleSchema = new mongoose.Schema({
  number: {type: String, index: {unique: true}},
  type: String,
  color: String,
  batch: String,
  condition: mongoose.Schema.Types.Mixed,
  material_id: {type: mongoose.Schema.Types.ObjectId, ref: MaterialModel},
  note_id: {type: mongoose.Schema.Types.ObjectId, ref: NoteModel},
  user_id: {type: mongoose.Schema.Types.ObjectId, ref: UserModel},
  status: String
}, {minimize: false});

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

export default mongoose.model<any, mongoose.Model<any, any>>('sample', SampleSchema);