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,
      
      

      },

shopify_domain: {
        type: DataTypes.TEXT,
      
      

      },

shopify_storefront_url: {
        type: DataTypes.TEXT,
      
      

      },

currency_code: {
        type: DataTypes.TEXT,
      
      

      },

primary_locale: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

connected_at: {
        type: DataTypes.DATE,
      
      

      },

shopify_api_version: {
        type: DataTypes.TEXT,
      
      

      },

      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.pages, {
      as: 'pages_store',
      foreignKey: {
          name: 'storeId',
      },
      constraints: false,
    });


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


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



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



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





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




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


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


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



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


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



//end loop



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




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

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



  return stores;
};


