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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"approved",


"active",


"closed"

        ],

      },

budget_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,
    },
  );

  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.programs.hasMany(db.projects, {
      as: 'projects_program',
      foreignKey: {
          name: 'programId',
      },
      constraints: false,
    });









    db.programs.hasMany(db.grants, {
      as: 'grants_program',
      foreignKey: {
          name: 'programId',
      },
      constraints: false,
    });
















//end loop



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

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

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




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

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



  return programs;
};


