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

account_code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

account_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"asset",


"liability",


"equity",


"income",


"expense",


"contra_asset"

        ],

      },

normal_balance: {
        type: DataTypes.ENUM,
      
      

        values: [

"debit",


"credit"

        ],

      },

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

      },

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

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

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













//end loop



    db.chart_of_accounts.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return chart_of_accounts;
};


