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

name: {
        type: DataTypes.TEXT,
      
      

      },

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"government",


"donor",


"telecom_levy",


"state_program",


"other"

        ],

      },

reference_code: {
        type: DataTypes.TEXT,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

total_committed_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

effective_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

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

  funding_sources.associate = (db) => {


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
















    db.funding_sources.hasMany(db.budget_programs, {
      as: 'budget_programs_funding_source',
      foreignKey: {
          name: 'funding_sourceId',
      },
      constraints: false,
    });













    db.funding_sources.hasMany(db.programs, {
      as: 'programs_funding_source',
      foreignKey: {
          name: 'funding_sourceId',
      },
      constraints: false,
    });

























//end loop



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




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

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



  return funding_sources;
};


