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

account_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"bank",


"credit_card",


"cash",


"loan",


"equity",


"revenue",


"expense",


"asset",


"liability"

        ],

      },

account_name: {
        type: DataTypes.TEXT,
      
      

      },

account_number_masked: {
        type: DataTypes.TEXT,
      
      

      },

institution_name: {
        type: DataTypes.TEXT,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

current_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

balance_as_of: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  financial_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.financial_accounts.hasMany(db.transactions, {
      as: 'transactions_financial_account',
      foreignKey: {
          name: 'financial_accountId',
      },
      constraints: false,
    });
















//end loop



    db.financial_accounts.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.financial_accounts.belongsTo(db.data_connections, {
      as: 'data_connection',
      foreignKey: {
        name: 'data_connectionId',
      },
      constraints: false,
    });

    db.financial_accounts.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return financial_accounts;
};


