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

class CardOptionValue {
  constructor(opt) {
    this.section = null;
    this.value = null;
    this.count = 0;
    this.optionsId = null;

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

  fillObject(opt) {
    this.section = opt.section;
    this.value = opt.value;
    this.count = opt.count || 0;
    this.optionsId = opt.optionsId ? new ObjectId(opt.optionsId) : null;
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'card-option-value.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        section: { type: ['string', 'null'] },
        value: { type: 'string' },
        count: { type: 'number' },
        optionsId: { type: ['object', 'null'] },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = CardOptionValue;
