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

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

accountType: {
        type: DataTypes.ENUM,
      
      

        values: [

"ASSET",


"LIABILITY",


"EQUITY",


"REVENUE",


"COST_OF_SALES",


"EXPENSE",


"OTHER_INCOME",


"OTHER_EXPENSE"

        ],

      },

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

      },

normalBalance: {
        type: DataTypes.ENUM,
      
      

        values: [

"DEBIT",


"CREDIT"

        ],

      },

isActive: {
        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,
    },
  );

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


    db.accounts.hasMany(db.vendors, {
      as: 'vendors_defaultAccount',
      foreignKey: {
          name: 'defaultAccountId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.ap_invoices, {
      as: 'ap_invoices_glAccount',
      foreignKey: {
          name: 'glAccountId',
      },
      constraints: false,
    });
















//end loop



    db.accounts.belongsTo(db.accounts, {
      as: 'parent',
      foreignKey: {
        name: 'parentId',
      },
      constraints: false,
    });




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

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



  return accounts;
};


