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

name: {
        type: DataTypes.TEXT,
      
      

      },

project_code: {
        type: DataTypes.TEXT,
      
      

      },

implementing_entity: {
        type: DataTypes.TEXT,
      
      

      },

objectives: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

planned_budget: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

completion_percent: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"active",


"on_hold",


"completed",


"closed"

        ],

      },

risk_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

reporting_cycle: {
        type: DataTypes.TEXT,
      
      

      },

last_reported_at: {
        type: DataTypes.DATE,
      
      

      },

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

  projects.associate = (db) => {


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






























    db.projects.hasMany(db.project_milestones, {
      as: 'project_milestones_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.risks, {
      as: 'risks_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.issues, {
      as: 'issues_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.field_verifications, {
      as: 'field_verifications_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.contracts, {
      as: 'contracts_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });










    db.projects.hasMany(db.invoices, {
      as: 'invoices_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });





    db.projects.hasMany(db.obligations, {
      as: 'obligations_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });






//end loop



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

    db.projects.belongsTo(db.programs, {
      as: 'program',
      foreignKey: {
        name: 'programId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.provinces, {
      as: 'province',
      foreignKey: {
        name: 'provinceId',
      },
      constraints: false,
    });

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




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

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



  return projects;
};


