const { Schema, model } = require("mongoose");

const {
  TODO_ITEM_MODEL_NAME,
  TODO_AUTH_USER_MODEL_NAME,
  TODO_AUTH_COMMENTS_MODEL_NAME,
} = require("../../const");

const schema = new Schema({
  title: String,
  done: { type: Boolean, default: false },
  closed: Date,
  created: {
    type: Date,
    default: () => new Date().toISOString(),
  },
  comments: [
    { type: Schema.Types.ObjectId, ref: TODO_AUTH_COMMENTS_MODEL_NAME },
  ],
  createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
});

schema.set("toJSON", {
  virtuals: true,
  versionKey: false,
});

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

schema.method('addComment', async function (commentId) {
    this.comments.push(commentId)
    await this.save()
})
  
exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema);
