const { ObjectId } = require('bson');
const tv4 = require('tv4');

class LibraryItemReference {
  constructor(opt) {
    this._id = null;
    this.name = null;
    this.catalog = null;

    if (opt) {
      this.fillObject(opt);
    }
  }

  fillObject(opt) {
    this._id = new ObjectId(opt._id);
    this.name = opt.name;
    this.catalog = opt.catalog;
  }

  validate() {
    return tv4.validateMultiple(this, LibraryItemReference.schema());
  }

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'libraryItem-reference.schema.json',
      type: 'object',
      properties: {
        _id: { type: 'object' },
        name: { type: 'string' },
        catalog: { type: 'string' },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = LibraryItemReference;
