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,
      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

opening_cash_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

closing_cash_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

expected_cash_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

cash_variance_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

shift_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"closed"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

      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.carts, {
      as: 'carts_shift',
      foreignKey: {
          name: 'shiftId',
      },
      constraints: false,
    });



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















//end loop



    db.shifts.belongsTo(db.registers, {
      as: 'register',
      foreignKey: {
        name: 'registerId',
      },
      constraints: false,
    });

    db.shifts.belongsTo(db.users, {
      as: 'opened_by_user',
      foreignKey: {
        name: 'opened_by_userId',
      },
      constraints: false,
    });

    db.shifts.belongsTo(db.users, {
      as: 'closed_by_user',
      foreignKey: {
        name: 'closed_by_userId',
      },
      constraints: false,
    });

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




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

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



  return shifts;
};


