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

account_name: {
        type: DataTypes.TEXT,
      
      

      },

account_code: {
        type: DataTypes.TEXT,
      
      

      },

account_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"asset",


"liability",


"equity",


"income",


"expense",


"cogs"

        ],

      },

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

      },

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

  chart_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.chart_accounts.hasMany(db.journal_lines, {
      as: 'journal_lines_account',
      foreignKey: {
          name: 'accountId',
      },
      constraints: false,
    });


    db.chart_accounts.hasMany(db.expenses, {
      as: 'expenses_expense_account',
      foreignKey: {
          name: 'expense_accountId',
      },
      constraints: false,
    });







//end loop



    db.chart_accounts.belongsTo(db.chart_accounts, {
      as: 'parent_account',
      foreignKey: {
        name: 'parent_accountId',
      },
      constraints: false,
    });

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




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

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



  return chart_accounts;
};


