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

label: {
        type: DataTypes.TEXT,
      
      

      },

recipient_name: {
        type: DataTypes.TEXT,
      
      

      },

recipient_phone: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

area: {
        type: DataTypes.TEXT,
      
      

      },

street_address: {
        type: DataTypes.TEXT,
      
      

      },

landmark: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  user_addresses.associate = (db) => {


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

























    db.user_addresses.hasMany(db.orders, {
      as: 'orders_shipping_address',
      foreignKey: {
          name: 'shipping_addressId',
      },
      constraints: false,
    });


















//end loop



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




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

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



  return user_addresses;
};


