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

display_name: {
        type: DataTypes.TEXT,
      
      

      },

company_name: {
        type: DataTypes.TEXT,
      
      

      },

contact_name: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

ice: {
        type: DataTypes.TEXT,
      
      

      },

if_number: {
        type: DataTypes.TEXT,
      
      

      },

rc: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        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,
    },
  );

  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.quotes, {
      as: 'quotes_client',
      foreignKey: {
          name: 'clientId',
      },
      constraints: false,
    });



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



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



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




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














//end loop



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

    db.clients.belongsTo(db.payment_terms, {
      as: 'payment_terms',
      foreignKey: {
        name: 'payment_termsId',
      },
      constraints: false,
    });




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

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



  return clients;
};


