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 storage_objects = sequelize.define(
    'storage_objects',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

object_key: {
        type: DataTypes.TEXT,
      
      

      },

content_type: {
        type: DataTypes.TEXT,
      
      

      },

size_bytes: {
        type: DataTypes.INTEGER,
      
      

      },

etag: {
        type: DataTypes.TEXT,
      
      

      },

sha256: {
        type: DataTypes.TEXT,
      
      

      },

compression_ratio: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"uploading",


"ready",


"processing",


"failed",


"deleted",


"trashed"

        ],

      },

trashed_at: {
        type: DataTypes.DATE,
      
      

      },

purge_after: {
        type: DataTypes.DATE,
      
      

      },

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

  storage_objects.associate = (db) => {


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












    db.storage_objects.hasMany(db.media_items, {
      as: 'media_items_storage_object',
      foreignKey: {
          name: 'storage_objectId',
      },
      constraints: false,
    });






























//end loop



    db.storage_objects.belongsTo(db.storage_buckets, {
      as: 'bucket',
      foreignKey: {
        name: 'bucketId',
      },
      constraints: false,
    });

    db.storage_objects.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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

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

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


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

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



  return storage_objects;
};


