const { Schema, model } = require('mongoose')

const {
  SMOKE_TRACKER_CIGARETTE_MODEL_NAME,
  SMOKE_TRACKER_USER_MODEL_NAME,
} = require('../const')

const schema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: SMOKE_TRACKER_USER_MODEL_NAME, required: true },
  smokedAt: {
    type: Date,
    required: true,
    default: () => new Date().toISOString(),
  },
  note: {
    type: String,
  },
  created: {
    type: Date,
    default: () => new Date().toISOString(),
  },
})

schema.set('toJSON', {
  virtuals: true,
  versionKey: false,
  transform: function (doc, ret) {
    delete ret._id
  },
})

schema.virtual('id').get(function () {
  return this._id.toHexString()
})

exports.CigaretteModel = model(SMOKE_TRACKER_CIGARETTE_MODEL_NAME, schema)


