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

code: {
        type: DataTypes.TEXT,

      },

principal_amount: {
        type: DataTypes.DECIMAL,

      },

term: {
        type: DataTypes.INTEGER,

      },

pending_amount: {
        type: DataTypes.DECIMAL,

      },

last_pending_amount: {
        type: DataTypes.DECIMAL,

      },

rate: {
        type: DataTypes.DECIMAL,

      },

commission_rate: {
        type: DataTypes.DECIMAL,

      },

registration_date: {
        type: DataTypes.DATE,

      },

started_payment_date: {
        type: DataTypes.DATE,

      },

last_payment_date: {
        type: DataTypes.DATE,

      },

finish_payment_date: {
        type: DataTypes.DATE,

      },

finish_discount: {
        type: DataTypes.DECIMAL,

      },

finish_discount_amount: {
        type: DataTypes.DECIMAL,

      },

admin_rate: {
        type: DataTypes.DECIMAL,

      },

admin_amount: {
        type: DataTypes.DECIMAL,

      },

purpose: {
        type: DataTypes.TEXT,

      },

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

  loans.associate = (db) => {

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

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

  return loans;
};

