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,
      },

coding_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"government",


"private"

        ],

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

level: {
        type: DataTypes.ENUM,
      
      

        values: [

"group",


"general_ledger",


"subsidiary",


"detail"

        ],

      },

nature: {
        type: DataTypes.ENUM,
      
      

        values: [

"debit",


"credit",


"mixed"

        ],

      },

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

      },

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

      },

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














//end loop



    db.accounts.belongsTo(db.businesses, {
      as: 'business',
      foreignKey: {
        name: 'businessId',
      },
      constraints: false,
    });




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

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



  return accounts;
};


