const tv4 = require('tv4');

const BI_FILTER = require('../../constant/analytics/bi-filter.constant');
const ARTIFACT_TYPE = require('../../constant/analytics/artifact-type.constant');

class Artifact {
  constructor(opt = {}) {
    this.id = null;
    this.name = null;
    this.type = null;
    this.category = null;
    this.subCategory = null;
    this.biWorkspaceId = null;
    this.biWorkspaceName = null;
    this.biId = null;
    this.biName = null;
    this.biReferenceId = null;
    this.biFilter = null;
    this.active = null;

    this.fillObject(opt);
  }

  fillObject(opt) {
    this.id = opt.id || null;
    this.name = opt.name || null;
    this.type = opt.type || null;
    this.category = opt.category || null;
    this.subCategory = opt.subCategory || null;
    this.biWorkspaceId = opt.biWorkspaceId || null;
    this.biWorkspaceName = opt.biWorkspaceName || null;
    this.biId = opt.biId || null;
    this.biName = opt.biName || null;
    this.biReferenceId = opt.biReferenceId || null;
    this.biFilter = opt.biFilter || null;
    this.active = opt.active || true;
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'artifact.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        id: { type: 'number' },
        name: { type: 'string', maxLength: 100 },
        type: { enum: ARTIFACT_TYPE.DROP_DOWN.map((type) => type.value) },
        category: { type: 'string', maxLength: 100 },
        subCategory: { type: 'string', maxLength: 100 },
        biWorkspaceId: { type: 'string', maxLength: 100 },
        biWorkspaceName: { type: 'string', maxLength: 100 },
        biId: { type: 'string', maxLength: 100 },
        biName: { type: 'string', maxLength: 100 },
        biReferenceId: { type: 'string', maxLength: 100 },
        biFilter: { enum: BI_FILTER.DROP_DOWN.map((type) => type.value) },
        active: { type: 'boolean' },
      },
      required: Object.keys(new Artifact()),
    };
  }
}

tv4.addSchema(Artifact.schema());

module.exports = Artifact;
