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

      account_name: {
        type: DataTypes.TEXT,
      },

      account_type: {
        type: DataTypes.ENUM,

        values: ['Asset', 'Liability', 'Equity', 'Revenue', 'Expense'],
      },

      balance: {
        type: DataTypes.DECIMAL,
      },

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

  financial_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.financial_accounts.hasMany(db.transactions, {
      as: 'transactions_account',
      foreignKey: {
        name: 'accountId',
      },
      constraints: false,
    });

    //end loop

    db.financial_accounts.belongsTo(db.companies, {
      as: 'companies',
      foreignKey: {
        name: 'companiesId',
      },
      constraints: false,
    });

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

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

  return financial_accounts;
};
