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,
      },

account_title: {
        type: DataTypes.TEXT,
      
      

      },

account_number: {
        type: DataTypes.TEXT,
      
      

      },

iban: {
        type: DataTypes.TEXT,
      
      

      },

card_number: {
        type: DataTypes.TEXT,
      
      

      },

opening_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

      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.pos_terminals, {
      as: 'pos_terminals_bank_account',
      foreignKey: {
          name: 'bank_accountId',
      },
      constraints: false,
    });






















//end loop



    db.bank_accounts.belongsTo(db.businesses, {
      as: 'business',
      foreignKey: {
        name: 'businessId',
      },
      constraints: false,
    });

    db.bank_accounts.belongsTo(db.banks, {
      as: 'bank',
      foreignKey: {
        name: 'bankId',
      },
      constraints: false,
    });

    db.bank_accounts.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

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




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

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



  return bank_accounts;
};


