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

class Pwa_cachesDBApi extends GenericDBApi {
  static get MODEL() {
    return db.pwa_caches;
  }

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

  static get SEARCHABLE_FIELDS() {
    return ['cache_version', 'manifest_json', 'asset_list_json'];
  }

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

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

  static get CSV_FIELDS() {
    return [
      'id',
      'environment',
      'cache_version',
      'is_active',
      'generated_at',
      'createdAt',
    ];
  }

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

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

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

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

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

  static getFieldMapping(data) {
    return {
      id: data.id || undefined,
      environment: data.environment || null,
      cache_version: data.cache_version || null,
      manifest_json: data.manifest_json || null,
      asset_list_json: data.asset_list_json || null,
      generated_at: data.generated_at || null,
      is_active: data.is_active || false,
    };
  }
}

module.exports = Pwa_cachesDBApi;
