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

original_filename: {
        type: DataTypes.TEXT,
      
      

      },

content_type: {
        type: DataTypes.TEXT,
      
      

      },

size_bytes: {
        type: DataTypes.INTEGER,
      
      

      },

checksum_sha256: {
        type: DataTypes.TEXT,
      
      

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

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

  file_uploads.associate = (db) => {


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










    db.file_uploads.hasMany(db.import_jobs, {
      as: 'import_jobs_file_upload',
      foreignKey: {
          name: 'file_uploadId',
      },
      constraints: false,
    });















//end loop



    db.file_uploads.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.file_uploads.belongsTo(db.users, {
      as: 'uploaded_by',
      foreignKey: {
        name: 'uploaded_byId',
      },
      constraints: false,
    });

    db.file_uploads.belongsTo(db.import_sources, {
      as: 'source',
      foreignKey: {
        name: 'sourceId',
      },
      constraints: false,
    });

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



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


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

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



  return file_uploads;
};


