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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

account_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"asset",


"liability",


"equity",


"income",


"expense"

        ],

      },

subtype: {
        type: DataTypes.ENUM,
      
      

        values: [

"bank",


"accounts_receivable",


"accounts_payable",


"cash",


"inventory",


"fixed_asset",


"tax_payable",


"revenue",


"cost_of_goods_sold",


"operating_expense",


"other"

        ],

      },

is_active: {
        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.products, {
      as: 'products_sales_account',
      foreignKey: {
          name: 'sales_accountId',
      },
      constraints: false,
    });

    db.accounts.hasMany(db.products, {
      as: 'products_purchase_account',
      foreignKey: {
          name: 'purchase_accountId',
      },
      constraints: false,
    });









    db.accounts.hasMany(db.bill_lines, {
      as: 'bill_lines_account',
      foreignKey: {
          name: 'accountId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.expenses, {
      as: 'expenses_account',
      foreignKey: {
          name: 'accountId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.bank_accounts, {
      as: 'bank_accounts_gl_account',
      foreignKey: {
          name: 'gl_accountId',
      },
      constraints: false,
    });





    db.accounts.hasMany(db.journal_lines, {
      as: 'journal_lines_account',
      foreignKey: {
          name: 'accountId',
      },
      constraints: false,
    });





//end loop



    db.accounts.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return accounts;
};


