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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"billing",


"shipping",


"warehouse",


"other"

        ],

      },

label: {
        type: DataTypes.TEXT,
      
      

      },

contact_name: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

line1: {
        type: DataTypes.TEXT,
      
      

      },

line2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  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.addresses.hasMany(db.customers, {
      as: 'customers_billing_address',
      foreignKey: {
          name: 'billing_addressId',
      },
      constraints: false,
    });

    db.addresses.hasMany(db.customers, {
      as: 'customers_shipping_address',
      foreignKey: {
          name: 'shipping_addressId',
      },
      constraints: false,
    });


    db.addresses.hasMany(db.suppliers, {
      as: 'suppliers_billing_address',
      foreignKey: {
          name: 'billing_addressId',
      },
      constraints: false,
    });

    db.addresses.hasMany(db.suppliers, {
      as: 'suppliers_shipping_address',
      foreignKey: {
          name: 'shipping_addressId',
      },
      constraints: false,
    });







    db.addresses.hasMany(db.warehouses, {
      as: 'warehouses_address',
      foreignKey: {
          name: 'addressId',
      },
      constraints: false,
    });














    db.addresses.hasMany(db.invoices, {
      as: 'invoices_billing_address',
      foreignKey: {
          name: 'billing_addressId',
      },
      constraints: false,
    });





//end loop



    db.addresses.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return addresses;
};


