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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

receipt_footer_note: {
        type: DataTypes.TEXT,
      
      

      },

      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.cash_drawer_sessions, {
      as: 'cash_drawer_sessions_register',
      foreignKey: {
          name: 'registerId',
      },
      constraints: false,
    });


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














//end loop



    db.registers.belongsTo(db.stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      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;
};


