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

name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"active",


"closed"

        ],

      },

estimated_total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  procurement_plans.associate = (db) => {


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





















    db.procurement_plans.hasMany(db.requisitions, {
      as: 'requisitions_procurement_plan',
      foreignKey: {
          name: 'procurement_planId',
      },
      constraints: false,
    });
































//end loop



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

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

    db.procurement_plans.belongsTo(db.departments, {
      as: 'department',
      foreignKey: {
        name: 'departmentId',
      },
      constraints: false,
    });




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

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



  return procurement_plans;
};


