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

funding_window_name: {
        type: DataTypes.TEXT,
      
      

      },

call_reference: {
        type: DataTypes.TEXT,
      
      

      },

call_open_at: {
        type: DataTypes.DATE,
      
      

      },

call_close_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"open",


"closed",


"evaluation",


"awarded",


"disbursing",


"completed",


"cancelled"

        ],

      },

eligibility_rules: {
        type: DataTypes.TEXT,
      
      

      },

total_envelope_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  grants.associate = (db) => {


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






































    db.grants.hasMany(db.beneficiaries, {
      as: 'beneficiaries_grant',
      foreignKey: {
          name: 'grantId',
      },
      constraints: false,
    });


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














//end loop



    db.grants.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.grants.belongsTo(db.programs, {
      as: 'program',
      foreignKey: {
        name: 'programId',
      },
      constraints: false,
    });




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

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



  return grants;
};


