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

class RoomSuite {
  constructor(opt) {
    this._id = new ObjectId();
    this.name = null;
    this.campusCode = null;
    this.tenantCode = null;
    this.buildingId = null;
    this.buildingName = null;
    this.rooms = [];

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

  fillObject(opt) {
    this._id = new ObjectId(opt._id);
    this.name = opt.name || null;
    this.campusCode = opt.campusCode || null;
    this.tenantCode = opt.tenantCode || null;
    this.buildingId = opt.buildingId || null;
    this.buildingName = opt.buildingName || null;

    if (opt.rooms) {
      opt.rooms.forEach((room) => this.rooms.push(room));
    }
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'room-suite.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        _id: { type: ['object', 'null'] },
        name: { type: ['string'] },
        campusCode: { type: ['string'] },
        tenantCode: { type: ['string'] },
        buildingId: { type: ['string'] },
        buildingName: { type: ['string'] },
        rooms: { type: 'array', item: { type: 'object' } },
      },
      required: Object.keys(new RoomSuite()),
    };
  }
}

module.exports = RoomSuite;
