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

business_name: {
        type: DataTypes.TEXT,
      
      

      },

merchant_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"shop_vendor",


"grocery_store",


"wholesaler"

        ],

      },

business_phone: {
        type: DataTypes.TEXT,
      
      

      },

support_email: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

verified_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"active",


"suspended"

        ],

      },

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

  merchant_profiles.associate = (db) => {


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








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




















//end loop



    db.merchant_profiles.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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



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

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


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

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



  return merchant_profiles;
};


