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

class Access_logsDBApi extends GenericDBApi {
  static get MODEL() {
    return db.access_logs;
  }

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

  static get SEARCHABLE_FIELDS() {
    return ['path', 'ip_address', 'user_agent'];
  }

  static get RANGE_FIELDS() {
    return ['accessed_at'];
  }

  static get ENUM_FIELDS() {
    return ['environment'];
  }

  static get CSV_FIELDS() {
    return [
      'id',
      'environment',
      'path',
      'ip_address',
      'user_agent',
      'accessed_at',
      'createdAt',
    ];
  }

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

  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,
      environment: data.environment || null,
      path: data.path || null,
      ip_address: data.ip_address || null,
      user_agent: data.user_agent || null,
      accessed_at: data.accessed_at || null,
    };
  }
}

module.exports = Access_logsDBApi;
