const GenericDBApi = require('./base.api');
const db = require('../models');

class Publish_eventsDBApi extends GenericDBApi {
  static get MODEL() {
    return db.publish_events;
  }

  static get TABLE_NAME() {
    return 'publish_events';
  }

  static get SEARCHABLE_FIELDS() {
    return ['title', 'description', 'error_message'];
  }

  static get RANGE_FIELDS() {
    return [
      'started_at',
      'finished_at',
      'pages_copied',
      'transitions_copied',
      'audios_copied',
    ];
  }

  static get ENUM_FIELDS() {
    return ['from_environment', 'to_environment', 'status'];
  }

  static get CSV_FIELDS() {
    return [
      'id',
      'title',
      'description',
      'from_environment',
      'to_environment',
      'status',
      'pages_copied',
      'createdAt',
    ];
  }

  static get AUTOCOMPLETE_FIELD() {
    return 'status';
  }

  static get ASSOCIATIONS() {
    return [
      { field: 'project', setter: 'setProject', isArray: false },
      { field: 'user', setter: 'setUser', isArray: false },
    ];
  }

  static get FIND_BY_INCLUDES() {
    return [{ association: 'project' }, { association: 'user' }];
  }

  static get FIND_ALL_INCLUDES() {
    return [
      { model: db.projects, as: 'project', required: false },
      { model: db.users, as: 'user', required: false },
    ];
  }

  static get RELATION_FILTERS() {
    return [
      {
        filterKey: 'project',
        model: db.projects,
        as: 'project',
        searchField: 'name',
      },
      {
        filterKey: 'user',
        model: db.users,
        as: 'user',
        searchField: 'firstName',
      },
    ];
  }

  static getFieldMapping(data) {
    return {
      id: data.id || undefined,
      title: data.title || null,
      description: data.description || null,
      from_environment: data.from_environment || null,
      to_environment: data.to_environment || null,
      started_at: data.started_at || null,
      finished_at: data.finished_at || null,
      status: data.status || null,
      error_message: data.error_message || null,
      pages_copied: data.pages_copied || null,
      transitions_copied: data.transitions_copied || null,
      audios_copied: data.audios_copied || null,
    };
  }
}

module.exports = Publish_eventsDBApi;
