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

tranche_number: {
        type: DataTypes.INTEGER,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

planned_disbursement_at: {
        type: DataTypes.DATE,
      
      

      },

disbursed_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"pending_approval",


"approved",


"disbursed",


"on_hold",


"cancelled"

        ],

      },

conditions: {
        type: DataTypes.TEXT,
      
      

      },

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

  grant_tranches.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity




















































//end loop



    db.grant_tranches.belongsTo(db.grant_applications, {
      as: 'application',
      foreignKey: {
        name: 'applicationId',
      },
      constraints: false,
    });

    db.grant_tranches.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return grant_tranches;
};


