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

name: {
        type: DataTypes.TEXT,
      
      

      },

beneficiary_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"ngo",


"school",


"community_center",


"local_government",


"public_institution",


"operator",


"other"

        ],

      },

registration_number: {
        type: DataTypes.TEXT,
      
      

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

approved_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"applicant",


"under_review",


"approved",


"rejected",


"active",


"suspended",


"closed"

        ],

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  beneficiaries.associate = (db) => {


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







































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














//end loop



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

    db.beneficiaries.belongsTo(db.provinces, {
      as: 'province',
      foreignKey: {
        name: 'provinceId',
      },
      constraints: false,
    });

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




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

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



  return beneficiaries;
};


