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

file_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"bank_statement",


"invoice",


"receipt",


"csv",


"other"

        ],

      },

file_name: {
        type: DataTypes.TEXT,
      
      

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  files_library.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.files_library.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

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

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



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


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

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



  return files_library;
};


