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

method_name: {
        type: DataTypes.TEXT,
      
      

      },

base_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

free_shipping_threshold: {
        type: DataTypes.DECIMAL,
      
      

      },

service_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"same_day",


"next_day",


"standard"

        ],

      },

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

      },

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

  shipping_methods.associate = (db) => {


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

























    db.shipping_methods.hasMany(db.orders, {
      as: 'orders_shipping_method',
      foreignKey: {
          name: 'shipping_methodId',
      },
      constraints: false,
    });


















//end loop



    db.shipping_methods.belongsTo(db.shipping_carriers, {
      as: 'carrier',
      foreignKey: {
        name: 'carrierId',
      },
      constraints: false,
    });




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

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



  return shipping_methods;
};


