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

contact_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"customer",


"vendor",


"lead",


"other"

        ],

      },

display_name: {
        type: DataTypes.TEXT,
      
      

      },

company_name: {
        type: DataTypes.TEXT,
      
      

      },

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

tin: {
        type: DataTypes.TEXT,
      
      

      },

vrn: {
        type: DataTypes.TEXT,
      
      

      },

billing_address_line1: {
        type: DataTypes.TEXT,
      
      

      },

billing_address_line2: {
        type: DataTypes.TEXT,
      
      

      },

billing_city: {
        type: DataTypes.TEXT,
      
      

      },

billing_state_region: {
        type: DataTypes.TEXT,
      
      

      },

billing_postal_code: {
        type: DataTypes.TEXT,
      
      

      },

billing_country: {
        type: DataTypes.TEXT,
      
      

      },

shipping_address_line1: {
        type: DataTypes.TEXT,
      
      

      },

shipping_address_line2: {
        type: DataTypes.TEXT,
      
      

      },

shipping_city: {
        type: DataTypes.TEXT,
      
      

      },

shipping_state_region: {
        type: DataTypes.TEXT,
      
      

      },

shipping_postal_code: {
        type: DataTypes.TEXT,
      
      

      },

shipping_country: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  contacts.associate = (db) => {


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













    db.contacts.hasMany(db.quotes, {
      as: 'quotes_customer',
      foreignKey: {
          name: 'customerId',
      },
      constraints: false,
    });



    db.contacts.hasMany(db.invoices, {
      as: 'invoices_customer',
      foreignKey: {
          name: 'customerId',
      },
      constraints: false,
    });



    db.contacts.hasMany(db.payments, {
      as: 'payments_customer',
      foreignKey: {
          name: 'customerId',
      },
      constraints: false,
    });



    db.contacts.hasMany(db.expenses, {
      as: 'expenses_vendor',
      foreignKey: {
          name: 'vendorId',
      },
      constraints: false,
    });



    db.contacts.hasMany(db.projects, {
      as: 'projects_customer',
      foreignKey: {
          name: 'customerId',
      },
      constraints: false,
    });








//end loop



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




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

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



  return contacts;
};


