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

class AssetsDBApi extends GenericDBApi {
  static get MODEL() {
    return db.assets;
  }

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

  static get SEARCHABLE_FIELDS() {
    return ['name', 'cdn_url', 'storage_key', 'mime_type', 'checksum'];
  }

  static get RANGE_FIELDS() {
    return ['size_mb', 'width_px', 'height_px', 'duration_sec'];
  }

  static get ENUM_FIELDS() {
    return ['asset_type', 'type', 'is_public'];
  }

  static get UUID_FIELDS() {
    return ['projectId'];
  }

  static get CSV_FIELDS() {
    return [
      'id',
      'name',
      'asset_type',
      'type',
      'cdn_url',
      'storage_key',
      'mime_type',
      'size_mb',
      'createdAt',
    ];
  }

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

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

  static get FIND_BY_INCLUDES() {
    return [
      { association: 'asset_variants_asset' },
      { 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,
      name: data.name || null,
      asset_type: data.asset_type || null,
      type: data.type || 'general',
      cdn_url: data.cdn_url || null,
      storage_key: data.storage_key || null,
      mime_type: data.mime_type || null,
      size_mb: data.size_mb || null,
      width_px: data.width_px || null,
      height_px: data.height_px || null,
      duration_sec: data.duration_sec || null,
      checksum: data.checksum || null,
      is_public: data.is_public || false,
    };
  }
}

module.exports = AssetsDBApi;
