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

table_label: {
        type: DataTypes.TEXT,
      
      

      },

seat_count: {
        type: DataTypes.INTEGER,
      
      

      },

qr_payload: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  restaurant_tables.associate = (db) => {


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














    db.restaurant_tables.hasMany(db.orders, {
      as: 'orders_table',
      foreignKey: {
          name: 'tableId',
      },
      constraints: false,
    });



    db.restaurant_tables.hasMany(db.reservations, {
      as: 'reservations_table',
      foreignKey: {
          name: 'tableId',
      },
      constraints: false,
    });














//end loop



    db.restaurant_tables.belongsTo(db.restaurants, {
      as: 'restaurant',
      foreignKey: {
        name: 'restaurantId',
      },
      constraints: false,
    });



    db.restaurant_tables.hasMany(db.file, {
      as: 'qr_image',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.restaurant_tables.getTableName(),
        belongsToColumn: 'qr_image',
      },
    });


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

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



  return restaurant_tables;
};


