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,
      
      

      },

account_name: {
        type: DataTypes.TEXT,
      
      

      },

account_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"asset",


"liability",


"equity",


"income",


"expense"

        ],

      },

category: {
        type: DataTypes.TEXT,
      
      

      },

normal_balance: {
        type: DataTypes.ENUM,
      
      

        values: [

"debit",


"credit"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

      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.invoice_lines, {
      as: 'invoice_lines_revenue_account',
      foreignKey: {
          name: 'revenue_accountId',
      },
      constraints: false,
    });


    db.chart_of_accounts.hasMany(db.payments, {
      as: 'payments_deposit_account',
      foreignKey: {
          name: 'deposit_accountId',
      },
      constraints: false,
    });




    db.chart_of_accounts.hasMany(db.vehicle_expenses, {
      as: 'vehicle_expenses_expense_account',
      foreignKey: {
          name: 'expense_accountId',
      },
      constraints: false,
    });

    db.chart_of_accounts.hasMany(db.vehicle_expenses, {
      as: 'vehicle_expenses_payment_account',
      foreignKey: {
          name: 'payment_accountId',
      },
      constraints: false,
    });


    db.chart_of_accounts.hasMany(db.driver_expenses, {
      as: 'driver_expenses_expense_account',
      foreignKey: {
          name: 'expense_accountId',
      },
      constraints: false,
    });

    db.chart_of_accounts.hasMany(db.driver_expenses, {
      as: 'driver_expenses_payment_account',
      foreignKey: {
          name: 'payment_accountId',
      },
      constraints: false,
    });



    db.chart_of_accounts.hasMany(db.bill_lines, {
      as: 'bill_lines_expense_account',
      foreignKey: {
          name: 'expense_accountId',
      },
      constraints: false,
    });


    db.chart_of_accounts.hasMany(db.bill_payments, {
      as: 'bill_payments_payment_account',
      foreignKey: {
          name: 'payment_accountId',
      },
      constraints: false,
    });


    db.chart_of_accounts.hasMany(db.ledger_entries, {
      as: 'ledger_entries_account',
      foreignKey: {
          name: 'accountId',
      },
      constraints: false,
    });







//end loop



    db.chart_of_accounts.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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




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

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



  return chart_of_accounts;
};


