const ObjectId = require('bson').ObjectID;
const tv4 = require('tv4');

const PersonReference = require('../core/person-reference');

class InventoryHazardSignReference {
  constructor(opt) {
    this._id = null;
    this.name = null;
    this.owner = null;
    this.lastCertifiedDate = null;

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

  fillObject(opt) {
    this._id = opt._id ? new ObjectId(opt._id) : null;
    this.name = opt.name;
    this.owner = opt.owner ? new PersonReference(opt.owner) : null;
    this.lastCertifiedDate = opt.lastCertifiedDate ? new Date(opt.lastCertifiedDate) : null;
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'inventory-hazard-sign-reference.schema.json',
      type: 'object',
      properties: {
        _id: { type: 'object' },
        name: { type: 'string' },
        owner: { $ref: 'person-reference.schema.json' },
        lastCertifiedDate: { type: ['object', null] },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = InventoryHazardSignReference;
