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

      name: {
        type: DataTypes.TEXT,
      },

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

  kooperatif.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.kooperatif.hasMany(db.users, {
      as: 'users_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.accounts, {
      as: 'accounts_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.announcements, {
      as: 'announcements_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.barter_offers, {
      as: 'barter_offers_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.delayed_payments, {
      as: 'delayed_payments_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.islamic_finance_library, {
      as: 'islamic_finance_library_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.loan_applications, {
      as: 'loan_applications_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.partners, {
      as: 'partners_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.projects, {
      as: 'projects_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    db.kooperatif.hasMany(db.transactions, {
      as: 'transactions_kooperatif',
      foreignKey: {
        name: 'kooperatifId',
      },
      constraints: false,
    });

    //end loop

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

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

  return kooperatif;
};
