const tv4 = require('tv4');

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

class GroupReference {
  constructor(opt) {
    this.id = null;
    this.name = null;
    this.owner = null;

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

  fillObject(opt) {
    this.id = opt.id;
    this.name = opt.name;
    this.owner = new PersonReference(opt.owner);
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'group-reference.schema.json',
      type: 'object',
      properties: {
        id: { type: 'string' },
        name: { type: 'string' },
        owner: { $ref: 'person-reference.schema.json' },
      },
      required: ['id', 'name', 'owner'],
    };
  }
}

tv4.addSchema(PersonReference.schema());

module.exports = GroupReference;
