import { isEmpty } from '@ember/utils';
import { get, computed } from '@ember/object';
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';

export default Model.extend({
  assessmentId: attr('number'),
  responseId: attr('number'),
  status: attr('string'),
  resourceId: attr('number'),
  resourceType: attr('string'),
  displayName: attr('string'),
  description: attr('string'),
  pageNumbers: attr('string'),
  created: attr('isodate'),
  updated: attr('isodate'),
  question: belongsTo('question'),
  discussions: hasMany('discussion'),

  // convert updated time to a unix timestamp
  updatedTimestamp: computed('updated', function() {
    return moment(get(this, 'updated')).unix();
  }),

  // is this a file-based verification
  isFile: computed('type', function() {
    const type = get(this, 'type');
    return type === 'file';
  }),

  // is this a url-based verification
  isUrl: computed('type', function() {
    const type = get(this, 'type');
    return type === 'url';
  }),

  // normalize the type of verification
  type: computed('resourceType', function() {
    const type = get(this, 'resourceType');
    return type.toLowerCase();
  }),

  // does the verification have discussions
  hasDiscussions: computed('discussions', function() {
    const discussions = get(this, 'discussions');
    if (!isEmpty(discussions)) {
      return true;
    }
  }),

  // is the verification approved
  isApproved: computed('status', function() {
    const status = get(this, 'status');
    if (isEmpty(status)) {
      return false;
    }

    return status.toLowerCase() === 'approved';
  }),

  // is the verification rejected
  isRejected: computed('status', function() {
    const status = get(this, 'status');
    if (isEmpty(status)) {
      return false;
    }

    return status.toLowerCase() === 'rejected';
  }),

  // is the verification active
  isActive: computed('status', function() {
    const status = get(this, 'status');
    if (isEmpty(status)) {
      return false;
    }

    return status.toLowerCase() === 'active';
  }),

  isDiscussionVisible: false
});
