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

name: {
        type: DataTypes.TEXT,
      
      

      },

timeIn: {
        type: DataTypes.TEXT,
      
      

      },

timeOut: {
        type: DataTypes.TEXT,
      
      

      },

breakDurationMinutes: {
        type: DataTypes.INTEGER,
      
      

      },

totalWorkingHours: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

rotationCycleDays: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  shifts.associate = (db) => {


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














    db.shifts.hasMany(db.employees, {
      as: 'employees_shift',
      foreignKey: {
          name: 'shiftId',
      },
      constraints: false,
    });



    db.shifts.hasMany(db.time_records, {
      as: 'time_records_shift',
      foreignKey: {
          name: 'shiftId',
      },
      constraints: false,
    });






























//end loop






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

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



  return shifts;
};


