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

application_reference: {
        type: DataTypes.TEXT,
      
      

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

requested_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

proposal_summary: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"eligible",


"ineligible",


"scoring",


"recommended",


"approved",


"rejected"

        ],

      },

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

  grant_applications.associate = (db) => {


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








































    db.grant_applications.hasMany(db.grant_evaluations, {
      as: 'grant_evaluations_application',
      foreignKey: {
          name: 'applicationId',
      },
      constraints: false,
    });


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












//end loop



    db.grant_applications.belongsTo(db.grants, {
      as: 'grant',
      foreignKey: {
        name: 'grantId',
      },
      constraints: false,
    });

    db.grant_applications.belongsTo(db.beneficiaries, {
      as: 'beneficiary',
      foreignKey: {
        name: 'beneficiaryId',
      },
      constraints: false,
    });

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




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

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



  return grant_applications;
};


