const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const media_assets = sequelize.define(
    'media_assets',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

asset_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"image",


"video",


"audio",


"file"

        ],

      },

mime_type: {
        type: DataTypes.TEXT,
      
      

      },

size_bytes: {
        type: DataTypes.INTEGER,
      
      

      },

width_px: {
        type: DataTypes.INTEGER,
      
      

      },

height_px: {
        type: DataTypes.INTEGER,
      
      

      },

duration_seconds: {
        type: DataTypes.DECIMAL,
      
      

      },

storage_key: {
        type: DataTypes.TEXT,
      
      

      },

checksum_text: {
        type: DataTypes.TEXT,
      
      

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  media_assets.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity



























//end loop



    db.media_assets.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



    db.media_assets.hasMany(db.file, {
      as: 'asset_files',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.media_assets.getTableName(),
        belongsToColumn: 'asset_files',
      },
    });

    db.media_assets.hasMany(db.file, {
      as: 'asset_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.media_assets.getTableName(),
        belongsToColumn: 'asset_images',
      },
    });


    db.media_assets.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.media_assets.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return media_assets;
};


