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

register_name: {
        type: DataTypes.TEXT,
      
      

      },

device_label: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  registers.associate = (db) => {


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
















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


    db.registers.hasMany(db.carts, {
      as: 'carts_register',
      foreignKey: {
          name: 'registerId',
      },
      constraints: false,
    });



    db.registers.hasMany(db.orders, {
      as: 'orders_register',
      foreignKey: {
          name: 'registerId',
      },
      constraints: false,
    });















//end loop



    db.registers.belongsTo(db.locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });

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




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

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



  return registers;
};


