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,
      
      

      },

location_name: {
        type: DataTypes.TEXT,
      
      

      },

latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

commodity: {
        type: DataTypes.ENUM,
      
      

        values: [

"gold",


"silver",


"copper",


"polymetallic",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"on_hold",


"completed",


"archived"

        ],

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

target_completion_date: {
        type: DataTypes.DATE,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  projects.associate = (db) => {

    db.projects.belongsToMany(db.users, {
      as: 'team_members',
      foreignKey: {
        name: 'projects_team_membersId',
      },
      constraints: false,
      through: 'projectsTeam_membersUsers',
    });

    db.projects.belongsToMany(db.users, {
      as: 'team_members_filter',
      foreignKey: {
        name: 'projects_team_membersId',
      },
      constraints: false,
      through: 'projectsTeam_membersUsers',
    });


/// 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_phases, {
      as: 'project_phases_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


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


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


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





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



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





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


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





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



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


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


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


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



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



//end loop



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




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

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



  return projects;
};


