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

transaction_date: {
        type: DataTypes.DATE,
      
      

      },

posted_at: {
        type: DataTypes.DATE,
      
      

      },

direction: {
        type: DataTypes.ENUM,
      
      

        values: [

"inflow",


"outflow"

        ],

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

reference: {
        type: DataTypes.TEXT,
      
      

      },

match_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unmatched",


"suggested",


"matched",


"excluded"

        ],

      },

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

  bank_transactions.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_transactions.hasMany(db.journal_entries, {
      as: 'journal_entries_source_bank_transaction',
      foreignKey: {
          name: 'source_bank_transactionId',
      },
      constraints: false,
    });






//end loop



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

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

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



    db.bank_transactions.hasMany(db.file, {
      as: 'import_source_file',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.bank_transactions.getTableName(),
        belongsToColumn: 'import_source_file',
      },
    });


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

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



  return bank_transactions;
};


