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

store_name: {
        type: DataTypes.TEXT,
      
      

      },

store_code: {
        type: DataTypes.TEXT,
      
      

      },

store_phone: {
        type: DataTypes.TEXT,
      
      

      },

store_email: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"open",


"closed",


"suspended"

        ],

      },

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

      },

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

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  stores.associate = (db) => {


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











    db.stores.hasMany(db.products, {
      as: 'products_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


    db.stores.hasMany(db.carts, {
      as: 'carts_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });



    db.stores.hasMany(db.orders, {
      as: 'orders_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });






    db.stores.hasMany(db.commissions, {
      as: 'commissions_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


    db.stores.hasMany(db.reviews, {
      as: 'reviews_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });








//end loop



    db.stores.belongsTo(db.merchant_profiles, {
      as: 'merchant_profile',
      foreignKey: {
        name: 'merchant_profileId',
      },
      constraints: false,
    });

    db.stores.belongsTo(db.addresses, {
      as: 'address',
      foreignKey: {
        name: 'addressId',
      },
      constraints: false,
    });

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



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


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

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



  return stores;
};


