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

const PersonReference = require('../core/person-reference');
const errorValidator = require('../../helper/flexible-questionnaire/fq-helper');

class FlexibleQuestionnaire {
  constructor(opt) {
    this._id = null;
    this.responses = null;
    this.templateId = null;
    this.createdBy = null;
    this.createdDate = null;
    this.lastModifiedBy = null;
    this.lastModifiedDate = null;
    if (opt) {
      this.fillBaseObject(opt);
    }
  }

  fillBaseObject(opt) {
    this._id = new ObjectId(opt._id);
    this.responses = opt.responses;
    this.templateId = opt.templateId;
    this.createdBy = new PersonReference(opt.createdBy);
    this.createdDate = opt.createdDate ? new Date(opt.createdDate) : null;
    this.lastModifiedBy = new PersonReference(opt.lastModifiedBy);
    this.lastModifiedDate = opt.lastModifiedDate ? new Date(opt.lastModifiedDate) : null;
  }

  // eslint-disable-next-line class-methods-use-this
  isValid(questions, answers) {
    return errorValidator.isFormValid(questions, answers);
  }
}

module.exports = FlexibleQuestionnaire;
