'use strict';
const { Model } = require('sequelize');
const { defaultKeys, modelDefaults, actionUsers } = require('../sequelize/defaults');
module.exports = (sequelize, DataTypes) => {
  class Asset extends Model {
    static associate(models) {
      actionUsers(Asset, models);
      Asset.belongsTo(models.User, {
        foreignKey: 'owner_id',
        targetKey: 'user_id',
        constraints: false,
        as: 'user',
      });
    }
  }
  Asset.init({
    ...defaultKeys("asset_id"),
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    type: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    data: {
      type: DataTypes.BLOB('long'),
      allowNull: false,
      get() {
        return
      }
    },
    mimetype: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    dataURI: {
      type: DataTypes.VIRTUAL,
      get() {
        const data = `data:${this.getDataValue('mimetype')};base64,${Buffer.from(this.getDataValue('data')).toString('base64')}`
        return data
      }
    }
  }, modelDefaults(sequelize, 'assets'));
  return Asset;
};