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

      balance: {
        type: DataTypes.DECIMAL,
      },

      debt_limit: {
        type: DataTypes.DECIMAL,
      },

      current_debt: {
        type: DataTypes.DECIMAL,
      },

      account_type: {
        type: DataTypes.ENUM,

        values: [
          'Karz-ıHasen',

          'KatılımHesabı',

          'TL',

          'EURO',

          'DOLAR',

          'ALTIN',

          'KatılımEmeklilikHesabı',
        ],
      },

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

    //end loop

    db.accounts.belongsTo(db.partners, {
      as: 'partner',
      foreignKey: {
        name: 'partnerId',
      },
      constraints: false,
    });

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

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

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

  return accounts;
};
