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

class RolesDBApi extends GenericDBApi {
  static get MODEL() {
    return db.roles;
  }

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

  static get SEARCHABLE_FIELDS() {
    return ['name', 'role_customization'];
  }

  static get RANGE_FIELDS() {
    return [];
  }

  static get ENUM_FIELDS() {
    return [];
  }

  static get CSV_FIELDS() {
    return ['id', 'name', 'role_customization', 'createdAt'];
  }

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

  static get ASSOCIATIONS() {
    return [{ field: 'permissions', setter: 'setPermissions', isArray: true }];
  }

  static get FIND_BY_INCLUDES() {
    return [{ association: 'users_app_role' }, { association: 'permissions' }];
  }

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

  static get RELATION_FILTERS() {
    return [
      {
        filterKey: 'permissions',
        model: db.permissions,
        as: 'permissions_filter',
        searchField: 'name',
      },
    ];
  }

  static getFieldMapping(data) {
    return {
      id: data.id || undefined,
      name: data.name || null,
      role_customization: data.role_customization || null,
    };
  }
}

module.exports = RolesDBApi;
