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

      title: {
        type: DataTypes.TEXT,
      },

      content: {
        type: DataTypes.TEXT,
      },

      resource_type: {
        type: DataTypes.ENUM,

        values: ['Article', 'Book', 'Resource'],
      },

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

  islamic_finance_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.islamic_finance_library.belongsTo(db.kooperatif, {
      as: 'kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

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

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

  return islamic_finance_library;
};
