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

subject_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"role",


"user",


"membership"

        ],

      },

permission: {
        type: DataTypes.ENUM,
      
      

        values: [

"view",


"upload",


"comment",


"like",


"download",


"manage"

        ],

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

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

  access_rules.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.access_rules.belongsTo(db.albums, {
      as: 'album',
      foreignKey: {
        name: 'albumId',
      },
      constraints: false,
    });

    db.access_rules.belongsTo(db.roles, {
      as: 'role',
      foreignKey: {
        name: 'roleId',
      },
      constraints: false,
    });

    db.access_rules.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.access_rules.belongsTo(db.memberships, {
      as: 'membership',
      foreignKey: {
        name: 'membershipId',
      },
      constraints: false,
    });

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




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

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



  return access_rules;
};


