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",


"supplier",


"both",


"employee",


"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,
      
      

      },

billing_address: {
        type: DataTypes.TEXT,
      
      

      },

shipping_address: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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.bills, {
      as: 'bills_supplier',
      foreignKey: {
          name: 'supplierId',
      },
      constraints: false,
    });



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




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



    db.contacts.hasMany(db.journal_lines, {
      as: 'journal_lines_contact',
      foreignKey: {
          name: 'contactId',
      },
      constraints: false,
    });


    db.contacts.hasMany(db.documents, {
      as: 'documents_contact',
      foreignKey: {
          name: 'contactId',
      },
      constraints: false,
    });




//end loop



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

    db.contacts.belongsTo(db.currencies, {
      as: 'currency',
      foreignKey: {
        name: 'currencyId',
      },
      constraints: false,
    });

    db.contacts.belongsTo(db.tax_rates, {
      as: 'default_tax_rate',
      foreignKey: {
        name: 'default_tax_rateId',
      },
      constraints: false,
    });




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

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



  return contacts;
};


