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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

objective: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"active",


"closed"

        ],

      },

approved_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

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

  budget_programs.associate = (db) => {


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

















    db.budget_programs.hasMany(db.budget_lines, {
      as: 'budget_lines_budget_program',
      foreignKey: {
          name: 'budget_programId',
      },
      constraints: false,
    });




































//end loop



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

    db.budget_programs.belongsTo(db.fiscal_years, {
      as: 'fiscal_year',
      foreignKey: {
        name: 'fiscal_yearId',
      },
      constraints: false,
    });

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




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

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



  return budget_programs;
};


