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,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"active",


"on_hold",


"completed",


"cancelled"

        ],

      },

delivery_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"agile",


"waterfall",


"hybrid"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

budget_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

budget_currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"CDF",


"EUR",


"XAF",


"XOF"

        ],

      },

external_costs: {
        type: DataTypes.DECIMAL,
      
      

      },

estimated_hours: {
        type: DataTypes.DECIMAL,
      
      

      },

actual_hours: {
        type: DataTypes.DECIMAL,
      
      

      },

risk_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

      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.milestones, {
      as: 'milestones_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


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



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


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


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





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





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




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





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









//end loop



    db.projects.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.deals, {
      as: 'deal',
      foreignKey: {
        name: 'dealId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.project_templates, {
      as: 'template',
      foreignKey: {
        name: 'templateId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.service_lines, {
      as: 'service_line',
      foreignKey: {
        name: 'service_lineId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.users, {
      as: 'project_manager',
      foreignKey: {
        name: 'project_managerId',
      },
      constraints: false,
    });

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




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

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



  return projects;
};


