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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"capex",


"opex",


"grant",


"consulting",


"infrastructure",


"equipment",


"services",


"other"

        ],

      },

approved_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

committed_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

disbursed_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

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

  budget_lines.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_lines.hasMany(db.allocations, {
      as: 'allocations_budget_line',
      foreignKey: {
          name: 'budget_lineId',
      },
      constraints: false,
    });


    db.budget_lines.hasMany(db.budget_reallocations, {
      as: 'budget_reallocations_from_budget_line',
      foreignKey: {
          name: 'from_budget_lineId',
      },
      constraints: false,
    });

    db.budget_lines.hasMany(db.budget_reallocations, {
      as: 'budget_reallocations_to_budget_line',
      foreignKey: {
          name: 'to_budget_lineId',
      },
      constraints: false,
    });



    db.budget_lines.hasMany(db.requisitions, {
      as: 'requisitions_budget_line',
      foreignKey: {
          name: 'budget_lineId',
      },
      constraints: false,
    });









    db.budget_lines.hasMany(db.projects, {
      as: 'projects_budget_line',
      foreignKey: {
          name: 'budget_lineId',
      },
      constraints: false,
    });






    db.budget_lines.hasMany(db.contracts, {
      as: 'contracts_budget_line',
      foreignKey: {
          name: 'budget_lineId',
      },
      constraints: false,
    });














    db.budget_lines.hasMany(db.obligations, {
      as: 'obligations_budget_line',
      foreignKey: {
          name: 'budget_lineId',
      },
      constraints: false,
    });






//end loop



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

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




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

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



  return budget_lines;
};


