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,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"todo",


"in_progress",


"review",


"blocked",


"done"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

estimated_hours: {
        type: DataTypes.DECIMAL,
      
      

      },

actual_hours: {
        type: DataTypes.DECIMAL,
      
      

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

      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




















    db.tasks.hasMany(db.task_dependencies, {
      as: 'task_dependencies_task',
      foreignKey: {
          name: 'taskId',
      },
      constraints: false,
    });

    db.tasks.hasMany(db.task_dependencies, {
      as: 'task_dependencies_depends_on_task',
      foreignKey: {
          name: 'depends_on_taskId',
      },
      constraints: false,
    });












    db.tasks.hasMany(db.time_entries, {
      as: 'time_entries_task',
      foreignKey: {
          name: 'taskId',
      },
      constraints: false,
    });
















//end loop



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

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

    db.tasks.belongsTo(db.milestones, {
      as: 'milestone',
      foreignKey: {
        name: 'milestoneId',
      },
      constraints: false,
    });

    db.tasks.belongsTo(db.users, {
      as: 'assignee',
      foreignKey: {
        name: 'assigneeId',
      },
      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;
};


