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

location_name: {
        type: DataTypes.TEXT,
      
      

      },

location_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"billing",


"shipping",


"both"

        ],

      },

address_line_1: {
        type: DataTypes.TEXT,
      
      

      },

address_line_2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

delivery_instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  locations.associate = (db) => {


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







    db.locations.hasMany(db.contacts, {
      as: 'contacts_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });








    db.locations.hasMany(db.carts, {
      as: 'carts_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });



    db.locations.hasMany(db.orders, {
      as: 'orders_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });




    db.locations.hasMany(db.quotes, {
      as: 'quotes_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });



    db.locations.hasMany(db.sample_requests, {
      as: 'sample_requests_location',
      foreignKey: {
          name: 'locationId',
      },
      constraints: false,
    });





//end loop



    db.locations.belongsTo(db.accounts, {
      as: 'account',
      foreignKey: {
        name: 'accountId',
      },
      constraints: false,
    });

    db.locations.belongsTo(db.contacts, {
      as: 'default_contact',
      foreignKey: {
        name: 'default_contactId',
      },
      constraints: false,
    });




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

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



  return locations;
};


