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

class FileReference {
  constructor(opt) {
    this.id = null;
    this.original = null;
    this.contentType = null;
    this.filename = null;
    this.thumbnail = null;
    this.token = null;

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

  fillObject(opt) {
    this.id = new ObjectId(opt.id);
    this.original = opt.original ? new ObjectId(opt.original) : null;
    this.contentType = opt.contentType ? opt.contentType : null;
    this.filename = opt.filename ? opt.filename : null;
    this.thumbnail = opt.thumbnail ? opt.thumbnail : null;
    this.token = opt.token || null;
  }

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'file-reference.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        id: { type: 'object' },
        original: { type: ['object', 'null'] },
        contentType: { type: ['string', 'null'] },
        filename: { type: ['string', 'null'] },
        thumbnail: { type: ['string', 'null'] },
        token: { type: ['string', 'null'] },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = FileReference;
