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

title: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"todo",


"in_progress",


"blocked",


"done",


"cancelled"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

entity_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"metallurgical_test",


"sample",


"flowsheet",


"economic_model",


"document",


"milestone",


"project_phase",


"none"

        ],

      },

entity_key: {
        type: DataTypes.TEXT,
      
      

      },

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

  tasks.associate = (db) => {


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


































//end loop



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

    db.tasks.belongsTo(db.project_phases, {
      as: 'phase',
      foreignKey: {
        name: 'phaseId',
      },
      constraints: false,
    });

    db.tasks.belongsTo(db.users, {
      as: 'assignee',
      foreignKey: {
        name: 'assigneeId',
      },
      constraints: false,
    });

    db.tasks.belongsTo(db.users, {
      as: 'reporter',
      foreignKey: {
        name: 'reporterId',
      },
      constraints: false,
    });

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




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

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



  return tasks;
};


