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_slug: {
        type: DataTypes.TEXT,
      
      

      },

store_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"umkm",


"brand",


"reseller",


"service_provider"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"paused",


"suspended"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

support_phone: {
        type: DataTypes.TEXT,
      
      

      },

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


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


















    db.stores.hasMany(db.affiliate_programs, {
      as: 'affiliate_programs_producer_store',
      foreignKey: {
          name: 'producer_storeId',
      },
      constraints: false,
    });











//end loop



    db.stores.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

    db.stores.belongsTo(db.addresses, {
      as: 'primary_address',
      foreignKey: {
        name: 'primary_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;
};


