const tv4 = require('tv4');

const ORGANIZATION_TYPES = require('../../constant/organization/organization-type.constant');

class Organization {
  constructor(opt) {
    this._id = null;
    this.campusCode = null;
    this.code = null;
    this.data = {};
    this.isActive = true;
    this.lastModifiedDate = null;
    this.name = null;
    this.type = null;
    if (opt) {
      this.fillObject(opt);
    }
  }

  fillObject(opt) {
    this._id = opt._id;
    this.campusCode = opt.campusCode;
    this.code = opt.code;
    this.data = opt.data;
    this.isActive = opt.isActive || true;
    this.lastModifiedDate = opt.lastModifiedDate ? new Date(opt.lastModifiedDate) : null;
    this.name = opt.name;
    this.type = opt.type;
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'organization.schema.json',
      type: 'object',
      properties: {
        _id: { type: 'string' },
        campusCode: { type: 'string' },
        code: { type: 'string' },
        data: { type: 'object' },
        isActive: { type: 'boolean' },
        lastModifiedDate: { type: 'object' },
        name: { type: 'string' },
        type: { enum: Object.keys(ORGANIZATION_TYPES) },
      },
      required: Object.keys(new Organization()),
    };
  }
}

module.exports = Organization;
