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

name: {
        type: DataTypes.TEXT,
      
      

      },

bank_name: {
        type: DataTypes.TEXT,
      
      

      },

account_number_masked: {
        type: DataTypes.TEXT,
      
      

      },

account_type_label: {
        type: DataTypes.TEXT,
      
      

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

last_sync_at: {
        type: DataTypes.DATE,
      
      

      },

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

  bank_accounts.associate = (db) => {


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






















    db.bank_accounts.hasMany(db.bank_transactions, {
      as: 'bank_transactions_bank_account',
      foreignKey: {
          name: 'bank_accountId',
      },
      constraints: false,
    });


    db.bank_accounts.hasMany(db.payments, {
      as: 'payments_bank_account',
      foreignKey: {
          name: 'bank_accountId',
      },
      constraints: false,
    });







//end loop



    db.bank_accounts.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.bank_accounts.belongsTo(db.currencies, {
      as: 'currency',
      foreignKey: {
        name: 'currencyId',
      },
      constraints: false,
    });

    db.bank_accounts.belongsTo(db.accounts, {
      as: 'gl_account',
      foreignKey: {
        name: 'gl_accountId',
      },
      constraints: false,
    });




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

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



  return bank_accounts;
};


