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

name: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

staff_count_planned: {
        type: DataTypes.INTEGER,
      
      

      },

table_count: {
        type: DataTypes.INTEGER,
      
      

      },

opening_time: {
        type: DataTypes.DATE,
      
      

      },

closing_time: {
        type: DataTypes.DATE,
      
      

      },

latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

geofence_radius_meters: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  restaurants.associate = (db) => {


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







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


    db.restaurants.hasMany(db.work_schedules, {
      as: 'work_schedules_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });



    db.restaurants.hasMany(db.attendance_events, {
      as: 'attendance_events_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.geofence_alerts, {
      as: 'geofence_alerts_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.menu_categories, {
      as: 'menu_categories_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.menu_items, {
      as: 'menu_items_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.orders, {
      as: 'orders_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });



    db.restaurants.hasMany(db.reservations, {
      as: 'reservations_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.payments, {
      as: 'payments_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });





    db.restaurants.hasMany(db.customer_reviews, {
      as: 'customer_reviews_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.notification_events, {
      as: 'notification_events_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.stock_locations, {
      as: 'stock_locations_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });



    db.restaurants.hasMany(db.stock_movements, {
      as: 'stock_movements_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.invoices, {
      as: 'invoices_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });


    db.restaurants.hasMany(db.audit_logs, {
      as: 'audit_logs_restaurant',
      foreignKey: {
          name: 'restaurantId',
      },
      constraints: false,
    });



//end loop






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

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



  return restaurants;
};


