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

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

duration_hours: {
        type: DataTypes.DECIMAL,
      
      

      },

billable: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

work_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"delivery",


"support",


"presales",


"internal",


"training"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

offline_recorded: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  time_entries.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.time_entries.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.time_entries.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.time_entries.belongsTo(db.timesheets, {
      as: 'timesheet',
      foreignKey: {
        name: 'timesheetId',
      },
      constraints: false,
    });

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

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

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

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

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




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

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



  return time_entries;
};


