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

store_name: {
        type: DataTypes.TEXT,
      
      

      },

store_description: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

policies_text: {
        type: DataTypes.TEXT,
      
      

      },

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

  merch_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.merch_stores.hasMany(db.creator_profiles, {
      as: 'creator_profiles_merch_store',
      foreignKey: {
          name: 'merch_storeId',
      },
      constraints: false,
    });




















    db.merch_stores.hasMany(db.products, {
      as: 'products_merch_store',
      foreignKey: {
          name: 'merch_storeId',
      },
      constraints: false,
    });



    db.merch_stores.hasMany(db.orders, {
      as: 'orders_merch_store',
      foreignKey: {
          name: 'merch_storeId',
      },
      constraints: false,
    });



    db.merch_stores.hasMany(db.shipping_profiles, {
      as: 'shipping_profiles_merch_store',
      foreignKey: {
          name: 'merch_storeId',
      },
      constraints: false,
    });








//end loop



    db.merch_stores.belongsTo(db.creator_profiles, {
      as: 'creator',
      foreignKey: {
        name: 'creatorId',
      },
      constraints: false,
    });

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



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

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


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

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



  return merch_stores;
};


