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

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"individual",


"business"

        ],

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

tax_id: {
        type: DataTypes.TEXT,
      
      

      },

credit_limit: {
        type: DataTypes.DECIMAL,
      
      

      },

opening_balance: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

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

  clients.associate = (db) => {


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










    db.clients.hasMany(db.stock_movements, {
      as: 'stock_movements_client',
      foreignKey: {
          name: 'clientId',
      },
      constraints: false,
    });


    db.clients.hasMany(db.quotes, {
      as: 'quotes_client',
      foreignKey: {
          name: 'clientId',
      },
      constraints: false,
    });



    db.clients.hasMany(db.sales, {
      as: 'sales_client',
      foreignKey: {
          name: 'clientId',
      },
      constraints: false,
    });



    db.clients.hasMany(db.invoices, {
      as: 'invoices_client',
      foreignKey: {
          name: 'clientId',
      },
      constraints: false,
    });



    db.clients.hasMany(db.payments, {
      as: 'payments_client',
      foreignKey: {
          name: 'clientId',
      },
      constraints: false,
    });







    db.clients.hasMany(db.alerts, {
      as: 'alerts_client',
      foreignKey: {
          name: 'clientId',
      },
      constraints: false,
    });





//end loop






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

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



  return clients;
};


